use ::libc;
extern "C" {
    pub type hyperlinks;
    pub type screen_write_cline;
    pub type screen_sel;
    pub type screen_titles;
    pub type event_base;
    pub type evbuffer;
    pub type bufferevent_ops;
    pub type args;
    pub type tmuxpeer;
    pub type environ;
    pub type options;
    pub type format_tree;
    pub type input_ctx;
    pub type cmds;
    pub type tty_key;
    pub type tty_code;
    pub type format_job_tree;
    pub type control_state;
    pub type cmdq_list;
    pub type cmdq_item;
    fn ioctl(__fd: i32, __request: u64, _: ...) -> i32;
    fn __errno_location() -> *mut i32;
    fn fcntl(__fd: i32, __cmd: i32, _: ...) -> i32;
    fn open(__file: *const i8, __oflag: i32, _: ...) -> i32;
    fn write(__fd: i32, __buf: *const libc::c_void, __n: size_t) -> ssize_t;
    fn usleep(__useconds: __useconds_t) -> i32;
    fn getpid() -> __pid_t;
    fn isatty(__fd: i32) -> i32;
    fn __b64_ntop(
        _: *const u8,
        _: size_t,
        _: *mut i8,
        _: size_t,
    ) -> i32;
    fn free(_: *mut libc::c_void);
    fn abs(_: i32) -> i32;
    fn memcpy(_: *mut libc::c_void, _: *const libc::c_void, _: u64) -> *mut libc::c_void;
    fn memset(_: *mut libc::c_void, _: i32, _: u64) -> *mut libc::c_void;
    fn strcmp(_: *const i8, _: *const i8) -> i32;
    fn strncmp(_: *const i8, _: *const i8, _: u64) -> i32;
    fn strlen(_: *const i8) -> u64;
    fn strerror(_: i32) -> *mut i8;
    fn tcgetattr(__fd: i32, __termios_p: *mut termios) -> i32;
    fn tcsetattr(
        __fd: i32,
        __optional_actions: i32,
        __termios_p: *const termios,
    ) -> i32;
    fn tcflush(__fd: i32, __queue_selector: i32) -> i32;
    fn time(__timer: *mut time_t) -> time_t;
    static mut global_options: *mut options;
    fn setblocking(_: i32, _: i32);
    fn format_create(
        _: *mut client,
        _: *mut cmdq_item,
        _: i32,
        _: i32,
    ) -> *mut format_tree;
    fn format_free(_: *mut format_tree);
    fn format_defaults(
        _: *mut format_tree,
        _: *mut client,
        _: *mut session,
        _: *mut winlink,
        _: *mut window_pane,
    );
    fn options_get_string(_: *mut options, _: *const i8) -> *const i8;
    fn options_get_number(_: *mut options, _: *const i8) -> i64;
    fn tty_term_apply_overrides(_: *mut tty_term);
    fn tty_term_create(
        _: *mut tty,
        _: *mut i8,
        _: *mut *mut i8,
        _: u_int,
        _: *mut i32,
        _: *mut *mut i8,
    ) -> *mut tty_term;
    fn tty_term_free(_: *mut tty_term);
    fn tty_term_has(_: *mut tty_term, _: tty_code_code) -> i32;
    fn tty_term_string(_: *mut tty_term, _: tty_code_code) -> *const i8;
    fn tty_term_string_i(_: *mut tty_term, _: tty_code_code, _: i32)
        -> *const i8;
    fn tty_term_string_ii(
        _: *mut tty_term,
        _: tty_code_code,
        _: i32,
        _: i32,
    ) -> *const i8;
    fn tty_term_string_iii(
        _: *mut tty_term,
        _: tty_code_code,
        _: i32,
        _: i32,
        _: i32,
    ) -> *const i8;
    fn tty_term_string_s(
        _: *mut tty_term,
        _: tty_code_code,
        _: *const i8,
    ) -> *const i8;
    fn tty_term_string_ss(
        _: *mut tty_term,
        _: tty_code_code,
        _: *const i8,
        _: *const i8,
    ) -> *const i8;
    fn tty_term_number(_: *mut tty_term, _: tty_code_code) -> i32;
    fn tty_term_flag(_: *mut tty_term, _: tty_code_code) -> i32;
    fn tty_apply_features(_: *mut tty_term, _: i32) -> i32;
    fn tty_acs_needed(_: *mut tty) -> i32;
    fn tty_acs_get(_: *mut tty, _: u_char) -> *const i8;
    fn tty_acs_reverse_get(_: *mut tty, _: *const i8, _: size_t) -> i32;
    fn tty_keys_build(_: *mut tty);
    fn tty_keys_free(_: *mut tty);
    fn tty_keys_next(_: *mut tty) -> i32;
    static mut clients: clients;
    fn server_client_lost(_: *mut client);
    fn server_client_get_pane(_: *mut client) -> *mut window_pane;
    fn server_redraw_client(_: *mut client);
    fn status_line_size(_: *mut client) -> u_int;
    fn colour_find_rgb(_: u_char, _: u_char, _: u_char) -> i32;
    fn colour_split_rgb(_: i32, _: *mut u_char, _: *mut u_char, _: *mut u_char);
    fn colour_force_rgb(_: i32) -> i32;
    fn colour_256to16(_: i32) -> i32;
    fn colour_palette_get(_: *mut colour_palette, _: i32) -> i32;
    static grid_default_cell: grid_cell;
    fn grid_cells_equal(_: *const grid_cell, _: *const grid_cell) -> i32;
    fn grid_get_line(_: *mut grid, _: u_int) -> *mut grid_line;
    fn fatal(_: *const i8, _: ...) -> !;
    fn log_debug(_: *const i8, _: ...);
    fn hyperlinks_get(
        _: *mut hyperlinks,
        _: u_int,
        _: *mut *const i8,
        _: *mut *const i8,
        _: *mut *const i8,
    ) -> i32;
    fn xsnprintf(_: *mut i8, _: size_t, _: *const i8, _: ...) -> i32;
    fn xmalloc(_: size_t) -> *mut libc::c_void;
    fn utf8_set(_: *mut utf8_data, _: u_char);
    fn screen_mode_to_string(_: i32) -> *const i8;
    fn log_get_level() -> i32;
    fn screen_select_cell(_: *mut screen, _: *mut grid_cell, _: *const grid_cell);
    fn grid_view_get_cell(_: *mut grid, _: u_int, _: u_int, _: *mut grid_cell);
    fn fatalx(_: *const i8, _: ...) -> !;
    fn style_add(_: *mut grid_cell, _: *mut options, _: *const i8, _: *mut format_tree);
    fn evbuffer_read(buffer: *mut evbuffer, fd: i32, howmuch: i32) -> i32;
    fn evbuffer_write(buffer: *mut evbuffer, fd: i32) -> i32;
    fn evbuffer_drain(buf: *mut evbuffer, len: size_t) -> i32;
    fn evbuffer_add(buf: *mut evbuffer, data: *const libc::c_void, datlen: size_t) -> i32;
    fn evbuffer_get_length(buf: *const evbuffer) -> size_t;
    fn evbuffer_free(buf: *mut evbuffer);
    fn evbuffer_new() -> *mut evbuffer;
    fn event_set(
        _: *mut event,
        _: i32,
        _: i16,
        _: Option<unsafe extern "C" fn(i32, i16, *mut libc::c_void) -> ()>,
        _: *mut libc::c_void,
    );
    fn event_initialized(ev: *const event) -> i32;
    fn event_del(_: *mut event) -> i32;
    fn event_add(ev: *mut event, timeout: *const timeval) -> i32;
}
pub type __u_char = u8;
pub type __u_short = u16;
pub type __u_int = u32;
pub type __uint8_t = u8;
pub type __uint64_t = u64;
pub type __pid_t = i32;
pub type __time_t = i64;
pub type __useconds_t = u32;
pub type __suseconds_t = i64;
pub type __ssize_t = i64;
pub type u_char = __u_char;
pub type u_short = __u_short;
pub type u_int = __u_int;
pub type pid_t = __pid_t;
pub type ssize_t = __ssize_t;
pub type time_t = __time_t;
pub type size_t = u64;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct timeval {
    pub tv_sec: __time_t,
    pub tv_usec: __suseconds_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct winsize {
    pub ws_row: u16,
    pub ws_col: u16,
    pub ws_xpixel: u16,
    pub ws_ypixel: u16,
}
pub type uint8_t = __uint8_t;
pub type uint64_t = __uint64_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen {
    pub title: *mut i8,
    pub path: *mut i8,
    pub titles: *mut screen_titles,
    pub grid: *mut grid,
    pub cx: u_int,
    pub cy: u_int,
    pub cstyle: screen_cursor_style,
    pub default_cstyle: screen_cursor_style,
    pub ccolour: i32,
    pub default_ccolour: i32,
    pub rupper: u_int,
    pub rlower: u_int,
    pub mode: i32,
    pub default_mode: i32,
    pub saved_cx: u_int,
    pub saved_cy: u_int,
    pub saved_grid: *mut grid,
    pub saved_cell: grid_cell,
    pub saved_flags: i32,
    pub tabs: *mut bitstr_t,
    pub sel: *mut screen_sel,
    pub write_list: *mut screen_write_cline,
    pub hyperlinks: *mut hyperlinks,
}
pub type bitstr_t = u8;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_cell {
    pub data: utf8_data,
    pub attr: u_short,
    pub flags: u_char,
    pub fg: i32,
    pub bg: i32,
    pub us: i32,
    pub link: u_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct utf8_data {
    pub data: [u_char; 21],
    pub have: u_char,
    pub size: u_char,
    pub width: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid {
    pub flags: i32,
    pub sx: u_int,
    pub sy: u_int,
    pub hscrolled: u_int,
    pub hsize: u_int,
    pub hlimit: u_int,
    pub linedata: *mut grid_line,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_line {
    pub celldata: *mut grid_cell_entry,
    pub cellused: u_int,
    pub cellsize: u_int,
    pub extddata: *mut grid_extd_entry,
    pub extdsize: u_int,
    pub flags: i32,
    pub time: time_t,
}
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct grid_extd_entry {
    pub data: utf8_char,
    pub attr: u_short,
    pub flags: u_char,
    pub fg: i32,
    pub bg: i32,
    pub us: i32,
    pub link: u_int,
}
pub type utf8_char = u_int;
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct grid_cell_entry {
    pub c2rust_unnamed: C2RustUnnamed,
    pub flags: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed {
    pub offset: u_int,
    pub data: C2RustUnnamed_0,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_0 {
    pub attr: u_char,
    pub fg: u_char,
    pub bg: u_char,
    pub data: u_char,
}
pub type screen_cursor_style = u32;
pub const SCREEN_CURSOR_BAR: screen_cursor_style = 3;
pub const SCREEN_CURSOR_UNDERLINE: screen_cursor_style = 2;
pub const SCREEN_CURSOR_BLOCK: screen_cursor_style = 1;
pub const SCREEN_CURSOR_DEFAULT: screen_cursor_style = 0;
pub type cc_t = u8;
pub type speed_t = u32;
pub type tcflag_t = u32;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct termios {
    pub c_iflag: tcflag_t,
    pub c_oflag: tcflag_t,
    pub c_cflag: tcflag_t,
    pub c_lflag: tcflag_t,
    pub c_line: cc_t,
    pub c_cc: [cc_t; 32],
    pub c_ispeed: speed_t,
    pub c_ospeed: speed_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct event {
    pub ev_evcallback: event_callback,
    pub ev_timeout_pos: C2RustUnnamed_6,
    pub ev_fd: i32,
    pub ev_base: *mut event_base,
    pub ev_: C2RustUnnamed_1,
    pub ev_events: i16,
    pub ev_res: i16,
    pub ev_timeout: timeval,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed_1 {
    pub ev_io: C2RustUnnamed_4,
    pub ev_signal: C2RustUnnamed_2,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_2 {
    pub ev_signal_next: C2RustUnnamed_3,
    pub ev_ncalls: i16,
    pub ev_pncalls: *mut i16,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_3 {
    pub le_next: *mut event,
    pub le_prev: *mut *mut event,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_4 {
    pub ev_io_next: C2RustUnnamed_5,
    pub ev_timeout: timeval,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_5 {
    pub le_next: *mut event,
    pub le_prev: *mut *mut event,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed_6 {
    pub ev_next_with_common_timeout: C2RustUnnamed_7,
    pub min_heap_idx: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_7 {
    pub tqe_next: *mut event,
    pub tqe_prev: *mut *mut event,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct event_callback {
    pub evcb_active_next: C2RustUnnamed_9,
    pub evcb_flags: i16,
    pub evcb_pri: uint8_t,
    pub evcb_closure: uint8_t,
    pub evcb_cb_union: C2RustUnnamed_8,
    pub evcb_arg: *mut libc::c_void,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed_8 {
    pub evcb_callback:
        Option<unsafe extern "C" fn(i32, i16, *mut libc::c_void) -> ()>,
    pub evcb_selfcb: Option<unsafe extern "C" fn(*mut event_callback, *mut libc::c_void) -> ()>,
    pub evcb_evfinalize: Option<unsafe extern "C" fn(*mut event, *mut libc::c_void) -> ()>,
    pub evcb_cbfinalize: Option<unsafe extern "C" fn(*mut event_callback, *mut libc::c_void) -> ()>,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_9 {
    pub tqe_next: *mut event_callback,
    pub tqe_prev: *mut *mut event_callback,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct bufferevent {
    pub ev_base: *mut event_base,
    pub be_ops: *const bufferevent_ops,
    pub ev_read: event,
    pub ev_write: event,
    pub input: *mut evbuffer,
    pub output: *mut evbuffer,
    pub wm_read: event_watermark,
    pub wm_write: event_watermark,
    pub readcb: bufferevent_data_cb,
    pub writecb: bufferevent_data_cb,
    pub errorcb: bufferevent_event_cb,
    pub cbarg: *mut libc::c_void,
    pub timeout_read: timeval,
    pub timeout_write: timeval,
    pub enabled: i16,
}
pub type bufferevent_event_cb =
    Option<unsafe extern "C" fn(*mut bufferevent, i16, *mut libc::c_void) -> ()>;
pub type bufferevent_data_cb =
    Option<unsafe extern "C" fn(*mut bufferevent, *mut libc::c_void) -> ()>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct event_watermark {
    pub low: size_t,
    pub high: size_t,
}
pub type msgtype = u32;
pub const MSG_READ_CANCEL: msgtype = 307;
pub const MSG_WRITE_CLOSE: msgtype = 306;
pub const MSG_WRITE_READY: msgtype = 305;
pub const MSG_WRITE: msgtype = 304;
pub const MSG_WRITE_OPEN: msgtype = 303;
pub const MSG_READ_DONE: msgtype = 302;
pub const MSG_READ: msgtype = 301;
pub const MSG_READ_OPEN: msgtype = 300;
pub const MSG_FLAGS: msgtype = 218;
pub const MSG_EXEC: msgtype = 217;
pub const MSG_WAKEUP: msgtype = 216;
pub const MSG_UNLOCK: msgtype = 215;
pub const MSG_SUSPEND: msgtype = 214;
pub const MSG_OLDSTDOUT: msgtype = 213;
pub const MSG_OLDSTDIN: msgtype = 212;
pub const MSG_OLDSTDERR: msgtype = 211;
pub const MSG_SHUTDOWN: msgtype = 210;
pub const MSG_SHELL: msgtype = 209;
pub const MSG_RESIZE: msgtype = 208;
pub const MSG_READY: msgtype = 207;
pub const MSG_LOCK: msgtype = 206;
pub const MSG_EXITING: msgtype = 205;
pub const MSG_EXITED: msgtype = 204;
pub const MSG_EXIT: msgtype = 203;
pub const MSG_DETACHKILL: msgtype = 202;
pub const MSG_DETACH: msgtype = 201;
pub const MSG_COMMAND: msgtype = 200;
pub const MSG_IDENTIFY_TERMINFO: msgtype = 112;
pub const MSG_IDENTIFY_LONGFLAGS: msgtype = 111;
pub const MSG_IDENTIFY_STDOUT: msgtype = 110;
pub const MSG_IDENTIFY_FEATURES: msgtype = 109;
pub const MSG_IDENTIFY_CWD: msgtype = 108;
pub const MSG_IDENTIFY_CLIENTPID: msgtype = 107;
pub const MSG_IDENTIFY_DONE: msgtype = 106;
pub const MSG_IDENTIFY_ENVIRON: msgtype = 105;
pub const MSG_IDENTIFY_STDIN: msgtype = 104;
pub const MSG_IDENTIFY_OLDCWD: msgtype = 103;
pub const MSG_IDENTIFY_TTYNAME: msgtype = 102;
pub const MSG_IDENTIFY_TERM: msgtype = 101;
pub const MSG_IDENTIFY_FLAGS: msgtype = 100;
pub const MSG_VERSION: msgtype = 12;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client {
    pub name: *const i8,
    pub peer: *mut tmuxpeer,
    pub queue: *mut cmdq_list,
    pub windows: client_windows,
    pub control_state: *mut control_state,
    pub pause_age: u_int,
    pub pid: pid_t,
    pub fd: i32,
    pub out_fd: i32,
    pub event: event,
    pub retval: i32,
    pub creation_time: timeval,
    pub activity_time: timeval,
    pub last_activity_time: timeval,
    pub environ: *mut environ,
    pub jobs: *mut format_job_tree,
    pub title: *mut i8,
    pub path: *mut i8,
    pub cwd: *const i8,
    pub term_name: *mut i8,
    pub term_features: i32,
    pub term_type: *mut i8,
    pub term_caps: *mut *mut i8,
    pub term_ncaps: u_int,
    pub ttyname: *mut i8,
    pub tty: tty,
    pub written: size_t,
    pub discarded: size_t,
    pub redraw: size_t,
    pub repeat_timer: event,
    pub click_timer: event,
    pub click_button: u_int,
    pub click_event: mouse_event,
    pub status: status_line,
    pub flags: uint64_t,
    pub exit_type: C2RustUnnamed_30,
    pub exit_msgtype: msgtype,
    pub exit_session: *mut i8,
    pub exit_message: *mut i8,
    pub keytable: *mut key_table,
    pub last_key: key_code,
    pub redraw_panes: uint64_t,
    pub redraw_scrollbars: uint64_t,
    pub message_ignore_keys: i32,
    pub message_ignore_styles: i32,
    pub message_string: *mut i8,
    pub message_timer: event,
    pub prompt_string: *mut i8,
    pub prompt_buffer: *mut utf8_data,
    pub prompt_last: *mut i8,
    pub prompt_index: size_t,
    pub prompt_inputcb: prompt_input_cb,
    pub prompt_freecb: prompt_free_cb,
    pub prompt_data: *mut libc::c_void,
    pub prompt_hindex: [u_int; 4],
    pub prompt_mode: C2RustUnnamed_27,
    pub prompt_saved: *mut utf8_data,
    pub prompt_flags: i32,
    pub prompt_type: prompt_type,
    pub prompt_cursor: i32,
    pub session: *mut session,
    pub last_session: *mut session,
    pub references: i32,
    pub pan_window: *mut libc::c_void,
    pub pan_ox: u_int,
    pub pan_oy: u_int,
    pub overlay_check: overlay_check_cb,
    pub overlay_mode: overlay_mode_cb,
    pub overlay_draw: overlay_draw_cb,
    pub overlay_key: overlay_key_cb,
    pub overlay_free: overlay_free_cb,
    pub overlay_resize: overlay_resize_cb,
    pub overlay_data: *mut libc::c_void,
    pub overlay_timer: event,
    pub files: client_files,
    pub clipboard_panes: *mut u_int,
    pub clipboard_npanes: u_int,
    pub entry: C2RustUnnamed_10,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_10 {
    pub tqe_next: *mut client,
    pub tqe_prev: *mut *mut client,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client_files {
    pub rbh_root: *mut client_file,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client_file {
    pub c: *mut client,
    pub peer: *mut tmuxpeer,
    pub tree: *mut client_files,
    pub references: i32,
    pub stream: i32,
    pub path: *mut i8,
    pub buffer: *mut evbuffer,
    pub event: *mut bufferevent,
    pub fd: i32,
    pub error: i32,
    pub closed: i32,
    pub cb: client_file_cb,
    pub data: *mut libc::c_void,
    pub entry: C2RustUnnamed_11,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_11 {
    pub rbe_left: *mut client_file,
    pub rbe_right: *mut client_file,
    pub rbe_parent: *mut client_file,
    pub rbe_color: i32,
}
pub type client_file_cb = Option<
    unsafe extern "C" fn(
        *mut client,
        *const i8,
        i32,
        i32,
        *mut evbuffer,
        *mut libc::c_void,
    ) -> (),
>;
pub type overlay_resize_cb = Option<unsafe extern "C" fn(*mut client, *mut libc::c_void) -> ()>;
pub type overlay_free_cb = Option<unsafe extern "C" fn(*mut client, *mut libc::c_void) -> ()>;
pub type overlay_key_cb =
    Option<unsafe extern "C" fn(*mut client, *mut libc::c_void, *mut key_event) -> i32>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct key_event {
    pub key: key_code,
    pub m: mouse_event,
    pub buf: *mut i8,
    pub len: size_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct mouse_event {
    pub valid: i32,
    pub ignore: i32,
    pub key: key_code,
    pub statusat: i32,
    pub statuslines: u_int,
    pub x: u_int,
    pub y: u_int,
    pub b: u_int,
    pub lx: u_int,
    pub ly: u_int,
    pub lb: u_int,
    pub ox: u_int,
    pub oy: u_int,
    pub s: i32,
    pub w: i32,
    pub wp: i32,
    pub sgr_type: u_int,
    pub sgr_b: u_int,
}
pub type key_code = u64;
pub type overlay_draw_cb =
    Option<unsafe extern "C" fn(*mut client, *mut libc::c_void, *mut screen_redraw_ctx) -> ()>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen_redraw_ctx {
    pub c: *mut client,
    pub statuslines: u_int,
    pub statustop: i32,
    pub pane_status: i32,
    pub pane_lines: pane_lines,
    pub pane_scrollbars: i32,
    pub pane_scrollbars_pos: i32,
    pub no_pane_gc: grid_cell,
    pub no_pane_gc_set: i32,
    pub sx: u_int,
    pub sy: u_int,
    pub ox: u_int,
    pub oy: u_int,
}
pub type pane_lines = u32;
pub const PANE_LINES_NUMBER: pane_lines = 4;
pub const PANE_LINES_SIMPLE: pane_lines = 3;
pub const PANE_LINES_HEAVY: pane_lines = 2;
pub const PANE_LINES_DOUBLE: pane_lines = 1;
pub const PANE_LINES_SINGLE: pane_lines = 0;
pub type overlay_mode_cb = Option<
    unsafe extern "C" fn(*mut client, *mut libc::c_void, *mut u_int, *mut u_int) -> *mut screen,
>;
pub type overlay_check_cb = Option<
    unsafe extern "C" fn(
        *mut client,
        *mut libc::c_void,
        u_int,
        u_int,
        u_int,
        *mut overlay_ranges,
    ) -> (),
>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct overlay_ranges {
    pub px: [u_int; 3],
    pub nx: [u_int; 3],
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct session {
    pub id: u_int,
    pub name: *mut i8,
    pub cwd: *const i8,
    pub creation_time: timeval,
    pub last_attached_time: timeval,
    pub activity_time: timeval,
    pub last_activity_time: timeval,
    pub lock_timer: event,
    pub curw: *mut winlink,
    pub lastw: winlink_stack,
    pub windows: winlinks,
    pub statusat: i32,
    pub statuslines: u_int,
    pub options: *mut options,
    pub flags: i32,
    pub attached: u_int,
    pub tio: *mut termios,
    pub environ: *mut environ,
    pub references: i32,
    pub gentry: C2RustUnnamed_13,
    pub entry: C2RustUnnamed_12,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_12 {
    pub rbe_left: *mut session,
    pub rbe_right: *mut session,
    pub rbe_parent: *mut session,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_13 {
    pub tqe_next: *mut session,
    pub tqe_prev: *mut *mut session,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct winlinks {
    pub rbh_root: *mut winlink,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct winlink {
    pub idx: i32,
    pub session: *mut session,
    pub window: *mut window,
    pub flags: i32,
    pub entry: C2RustUnnamed_16,
    pub wentry: C2RustUnnamed_15,
    pub sentry: C2RustUnnamed_14,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_14 {
    pub tqe_next: *mut winlink,
    pub tqe_prev: *mut *mut winlink,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_15 {
    pub tqe_next: *mut winlink,
    pub tqe_prev: *mut *mut winlink,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_16 {
    pub rbe_left: *mut winlink,
    pub rbe_right: *mut winlink,
    pub rbe_parent: *mut winlink,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window {
    pub id: u_int,
    pub latest: *mut libc::c_void,
    pub name: *mut i8,
    pub name_event: event,
    pub name_time: timeval,
    pub alerts_timer: event,
    pub offset_timer: event,
    pub activity_time: timeval,
    pub active: *mut window_pane,
    pub last_panes: window_panes,
    pub panes: window_panes,
    pub lastlayout: i32,
    pub layout_root: *mut layout_cell,
    pub saved_layout_root: *mut layout_cell,
    pub old_layout: *mut i8,
    pub sx: u_int,
    pub sy: u_int,
    pub manual_sx: u_int,
    pub manual_sy: u_int,
    pub xpixel: u_int,
    pub ypixel: u_int,
    pub new_sx: u_int,
    pub new_sy: u_int,
    pub new_xpixel: u_int,
    pub new_ypixel: u_int,
    pub fill_character: *mut utf8_data,
    pub flags: i32,
    pub alerts_queued: i32,
    pub alerts_entry: C2RustUnnamed_19,
    pub options: *mut options,
    pub references: u_int,
    pub winlinks: C2RustUnnamed_18,
    pub entry: C2RustUnnamed_17,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_17 {
    pub rbe_left: *mut window,
    pub rbe_right: *mut window,
    pub rbe_parent: *mut window,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_18 {
    pub tqh_first: *mut winlink,
    pub tqh_last: *mut *mut winlink,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_19 {
    pub tqe_next: *mut window,
    pub tqe_prev: *mut *mut window,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct layout_cell {
    pub type_0: layout_type,
    pub parent: *mut layout_cell,
    pub sx: u_int,
    pub sy: u_int,
    pub xoff: u_int,
    pub yoff: u_int,
    pub wp: *mut window_pane,
    pub cells: layout_cells,
    pub entry: C2RustUnnamed_20,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_20 {
    pub tqe_next: *mut layout_cell,
    pub tqe_prev: *mut *mut layout_cell,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct layout_cells {
    pub tqh_first: *mut layout_cell,
    pub tqh_last: *mut *mut layout_cell,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_pane {
    pub id: u_int,
    pub active_point: u_int,
    pub window: *mut window,
    pub options: *mut options,
    pub layout_cell: *mut layout_cell,
    pub saved_layout_cell: *mut layout_cell,
    pub sx: u_int,
    pub sy: u_int,
    pub xoff: u_int,
    pub yoff: u_int,
    pub flags: i32,
    pub sb_slider_y: u_int,
    pub sb_slider_h: u_int,
    pub argc: i32,
    pub argv: *mut *mut i8,
    pub shell: *mut i8,
    pub cwd: *mut i8,
    pub pid: pid_t,
    pub tty: [i8; 32],
    pub status: i32,
    pub dead_time: timeval,
    pub fd: i32,
    pub event: *mut bufferevent,
    pub offset: window_pane_offset,
    pub base_offset: size_t,
    pub resize_queue: window_pane_resizes,
    pub resize_timer: event,
    pub ictx: *mut input_ctx,
    pub cached_gc: grid_cell,
    pub cached_active_gc: grid_cell,
    pub palette: colour_palette,
    pub pipe_fd: i32,
    pub pipe_event: *mut bufferevent,
    pub pipe_offset: window_pane_offset,
    pub screen: *mut screen,
    pub base: screen,
    pub status_screen: screen,
    pub status_size: size_t,
    pub modes: C2RustUnnamed_24,
    pub searchstr: *mut i8,
    pub searchregex: i32,
    pub border_gc_set: i32,
    pub border_gc: grid_cell,
    pub control_bg: i32,
    pub control_fg: i32,
    pub scrollbar_style: style,
    pub entry: C2RustUnnamed_23,
    pub sentry: C2RustUnnamed_22,
    pub tree_entry: C2RustUnnamed_21,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_21 {
    pub rbe_left: *mut window_pane,
    pub rbe_right: *mut window_pane,
    pub rbe_parent: *mut window_pane,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_22 {
    pub tqe_next: *mut window_pane,
    pub tqe_prev: *mut *mut window_pane,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_23 {
    pub tqe_next: *mut window_pane,
    pub tqe_prev: *mut *mut window_pane,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct style {
    pub gc: grid_cell,
    pub ignore: i32,
    pub fill: i32,
    pub align: style_align,
    pub list: style_list,
    pub range_type: style_range_type,
    pub range_argument: u_int,
    pub range_string: [i8; 16],
    pub width: i32,
    pub pad: i32,
    pub default_type: style_default_type,
}
pub type style_default_type = u32;
pub const STYLE_DEFAULT_POP: style_default_type = 2;
pub const STYLE_DEFAULT_PUSH: style_default_type = 1;
pub const STYLE_DEFAULT_BASE: style_default_type = 0;
pub type style_range_type = u32;
pub const STYLE_RANGE_USER: style_range_type = 6;
pub const STYLE_RANGE_SESSION: style_range_type = 5;
pub const STYLE_RANGE_WINDOW: style_range_type = 4;
pub const STYLE_RANGE_PANE: style_range_type = 3;
pub const STYLE_RANGE_RIGHT: style_range_type = 2;
pub const STYLE_RANGE_LEFT: style_range_type = 1;
pub const STYLE_RANGE_NONE: style_range_type = 0;
pub type style_list = u32;
pub const STYLE_LIST_RIGHT_MARKER: style_list = 4;
pub const STYLE_LIST_LEFT_MARKER: style_list = 3;
pub const STYLE_LIST_FOCUS: style_list = 2;
pub const STYLE_LIST_ON: style_list = 1;
pub const STYLE_LIST_OFF: style_list = 0;
pub type style_align = u32;
pub const STYLE_ALIGN_ABSOLUTE_CENTRE: style_align = 4;
pub const STYLE_ALIGN_RIGHT: style_align = 3;
pub const STYLE_ALIGN_CENTRE: style_align = 2;
pub const STYLE_ALIGN_LEFT: style_align = 1;
pub const STYLE_ALIGN_DEFAULT: style_align = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_24 {
    pub tqh_first: *mut window_mode_entry,
    pub tqh_last: *mut *mut window_mode_entry,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_mode_entry {
    pub wp: *mut window_pane,
    pub swp: *mut window_pane,
    pub mode: *const window_mode,
    pub data: *mut libc::c_void,
    pub screen: *mut screen,
    pub prefix: u_int,
    pub entry: C2RustUnnamed_25,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_25 {
    pub tqe_next: *mut window_mode_entry,
    pub tqe_prev: *mut *mut window_mode_entry,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_mode {
    pub name: *const i8,
    pub default_format: *const i8,
    pub init: Option<
        unsafe extern "C" fn(*mut window_mode_entry, *mut cmd_find_state, *mut args) -> *mut screen,
    >,
    pub free: Option<unsafe extern "C" fn(*mut window_mode_entry) -> ()>,
    pub resize: Option<unsafe extern "C" fn(*mut window_mode_entry, u_int, u_int) -> ()>,
    pub update: Option<unsafe extern "C" fn(*mut window_mode_entry) -> ()>,
    pub key: Option<
        unsafe extern "C" fn(
            *mut window_mode_entry,
            *mut client,
            *mut session,
            *mut winlink,
            key_code,
            *mut mouse_event,
        ) -> (),
    >,
    pub key_table: Option<unsafe extern "C" fn(*mut window_mode_entry) -> *const i8>,
    pub command: Option<
        unsafe extern "C" fn(
            *mut window_mode_entry,
            *mut client,
            *mut session,
            *mut winlink,
            *mut args,
            *mut mouse_event,
        ) -> (),
    >,
    pub formats: Option<unsafe extern "C" fn(*mut window_mode_entry, *mut format_tree) -> ()>,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct cmd_find_state {
    pub flags: i32,
    pub current: *mut cmd_find_state,
    pub s: *mut session,
    pub wl: *mut winlink,
    pub w: *mut window,
    pub wp: *mut window_pane,
    pub idx: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_pane_offset {
    pub used: size_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct colour_palette {
    pub fg: i32,
    pub bg: i32,
    pub palette: *mut i32,
    pub default_palette: *mut i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_pane_resizes {
    pub tqh_first: *mut window_pane_resize,
    pub tqh_last: *mut *mut window_pane_resize,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_pane_resize {
    pub sx: u_int,
    pub sy: u_int,
    pub osx: u_int,
    pub osy: u_int,
    pub entry: C2RustUnnamed_26,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_26 {
    pub tqe_next: *mut window_pane_resize,
    pub tqe_prev: *mut *mut window_pane_resize,
}
pub type layout_type = u32;
pub const LAYOUT_WINDOWPANE: layout_type = 2;
pub const LAYOUT_TOPBOTTOM: layout_type = 1;
pub const LAYOUT_LEFTRIGHT: layout_type = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_panes {
    pub tqh_first: *mut window_pane,
    pub tqh_last: *mut *mut window_pane,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct winlink_stack {
    pub tqh_first: *mut winlink,
    pub tqh_last: *mut *mut winlink,
}
pub type prompt_type = u32;
pub const PROMPT_TYPE_INVALID: prompt_type = 255;
pub const PROMPT_TYPE_WINDOW_TARGET: prompt_type = 3;
pub const PROMPT_TYPE_TARGET: prompt_type = 2;
pub const PROMPT_TYPE_SEARCH: prompt_type = 1;
pub const PROMPT_TYPE_COMMAND: prompt_type = 0;
pub type C2RustUnnamed_27 = u32;
pub const PROMPT_COMMAND: C2RustUnnamed_27 = 1;
pub const PROMPT_ENTRY: C2RustUnnamed_27 = 0;
pub type prompt_free_cb = Option<unsafe extern "C" fn(*mut libc::c_void) -> ()>;
pub type prompt_input_cb = Option<
    unsafe extern "C" fn(
        *mut client,
        *mut libc::c_void,
        *const i8,
        i32,
    ) -> i32,
>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct key_table {
    pub name: *const i8,
    pub activity_time: timeval,
    pub key_bindings: key_bindings,
    pub default_key_bindings: key_bindings,
    pub references: u_int,
    pub entry: C2RustUnnamed_28,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_28 {
    pub rbe_left: *mut key_table,
    pub rbe_right: *mut key_table,
    pub rbe_parent: *mut key_table,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct key_bindings {
    pub rbh_root: *mut key_binding,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct key_binding {
    pub key: key_code,
    pub cmdlist: *mut cmd_list,
    pub note: *const i8,
    pub flags: i32,
    pub entry: C2RustUnnamed_29,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_29 {
    pub rbe_left: *mut key_binding,
    pub rbe_right: *mut key_binding,
    pub rbe_parent: *mut key_binding,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct cmd_list {
    pub references: i32,
    pub group: u_int,
    pub list: *mut cmds,
}
pub type C2RustUnnamed_30 = u32;
pub const CLIENT_EXIT_DETACH: C2RustUnnamed_30 = 2;
pub const CLIENT_EXIT_SHUTDOWN: C2RustUnnamed_30 = 1;
pub const CLIENT_EXIT_RETURN: C2RustUnnamed_30 = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct status_line {
    pub timer: event,
    pub screen: screen,
    pub active: *mut screen,
    pub references: i32,
    pub style: grid_cell,
    pub entries: [status_line_entry; 5],
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct status_line_entry {
    pub expanded: *mut i8,
    pub ranges: style_ranges,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct style_ranges {
    pub tqh_first: *mut style_range,
    pub tqh_last: *mut *mut style_range,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct style_range {
    pub type_0: style_range_type,
    pub argument: u_int,
    pub string: [i8; 16],
    pub start: u_int,
    pub end: u_int,
    pub entry: C2RustUnnamed_31,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_31 {
    pub tqe_next: *mut style_range,
    pub tqe_prev: *mut *mut style_range,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct tty {
    pub client: *mut client,
    pub start_timer: event,
    pub clipboard_timer: event,
    pub last_requests: time_t,
    pub sx: u_int,
    pub sy: u_int,
    pub xpixel: u_int,
    pub ypixel: u_int,
    pub cx: u_int,
    pub cy: u_int,
    pub cstyle: screen_cursor_style,
    pub ccolour: i32,
    pub oflag: i32,
    pub oox: u_int,
    pub ooy: u_int,
    pub osx: u_int,
    pub osy: u_int,
    pub mode: i32,
    pub fg: i32,
    pub bg: i32,
    pub rlower: u_int,
    pub rupper: u_int,
    pub rleft: u_int,
    pub rright: u_int,
    pub event_in: event,
    pub in_0: *mut evbuffer,
    pub event_out: event,
    pub out: *mut evbuffer,
    pub timer: event,
    pub discarded: size_t,
    pub tio: termios,
    pub cell: grid_cell,
    pub last_cell: grid_cell,
    pub flags: i32,
    pub term: *mut tty_term,
    pub mouse_last_x: u_int,
    pub mouse_last_y: u_int,
    pub mouse_last_b: u_int,
    pub mouse_drag_flag: i32,
    pub mouse_scrolling_flag: i32,
    pub mouse_slider_mpos: i32,
    pub mouse_drag_update: Option<unsafe extern "C" fn(*mut client, *mut mouse_event) -> ()>,
    pub mouse_drag_release: Option<unsafe extern "C" fn(*mut client, *mut mouse_event) -> ()>,
    pub key_timer: event,
    pub key_tree: *mut tty_key,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct tty_term {
    pub name: *mut i8,
    pub tty: *mut tty,
    pub features: i32,
    pub acs: [[i8; 2]; 256],
    pub codes: *mut tty_code,
    pub flags: i32,
    pub entry: C2RustUnnamed_32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_32 {
    pub le_next: *mut tty_term,
    pub le_prev: *mut *mut tty_term,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client_windows {
    pub rbh_root: *mut client_window,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client_window {
    pub window: u_int,
    pub pane: *mut window_pane,
    pub sx: u_int,
    pub sy: u_int,
    pub entry: C2RustUnnamed_33,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_33 {
    pub rbe_left: *mut client_window,
    pub rbe_right: *mut client_window,
    pub rbe_parent: *mut client_window,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct tty_ctx {
    pub s: *mut screen,
    pub redraw_cb: tty_ctx_redraw_cb,
    pub set_client_cb: tty_ctx_set_client_cb,
    pub arg: *mut libc::c_void,
    pub cell: *const grid_cell,
    pub wrapped: i32,
    pub num: u_int,
    pub ptr: *mut libc::c_void,
    pub ptr2: *mut libc::c_void,
    pub allow_invisible_panes: i32,
    pub ocx: u_int,
    pub ocy: u_int,
    pub orupper: u_int,
    pub orlower: u_int,
    pub xoff: u_int,
    pub yoff: u_int,
    pub rxoff: u_int,
    pub ryoff: u_int,
    pub sx: u_int,
    pub sy: u_int,
    pub bg: u_int,
    pub defaults: grid_cell,
    pub palette: *mut colour_palette,
    pub bigger: i32,
    pub wox: u_int,
    pub woy: u_int,
    pub wsx: u_int,
    pub wsy: u_int,
}
pub type tty_ctx_set_client_cb =
    Option<unsafe extern "C" fn(*mut tty_ctx, *mut client) -> i32>;
pub type tty_ctx_redraw_cb = Option<unsafe extern "C" fn(*const tty_ctx) -> ()>;
pub type tty_code_code = u32;
pub const TTYC_XT: tty_code_code = 231;
pub const TTYC_VPA: tty_code_code = 230;
pub const TTYC_U8: tty_code_code = 229;
pub const TTYC_TSL: tty_code_code = 228;
pub const TTYC_TC: tty_code_code = 227;
pub const TTYC_SYNC: tty_code_code = 226;
pub const TTYC_SWD: tty_code_code = 225;
pub const TTYC_SS: tty_code_code = 224;
pub const TTYC_SXL: tty_code_code = 223;
pub const TTYC_SMXX: tty_code_code = 222;
pub const TTYC_SMULX: tty_code_code = 221;
pub const TTYC_SMUL: tty_code_code = 220;
pub const TTYC_SMSO: tty_code_code = 219;
pub const TTYC_SMOL: tty_code_code = 218;
pub const TTYC_SMKX: tty_code_code = 217;
pub const TTYC_SMCUP: tty_code_code = 216;
pub const TTYC_SMACS: tty_code_code = 215;
pub const TTYC_SITM: tty_code_code = 214;
pub const TTYC_SGR0: tty_code_code = 213;
pub const TTYC_SETULC1: tty_code_code = 212;
pub const TTYC_SETULC: tty_code_code = 211;
pub const TTYC_SETRGBF: tty_code_code = 210;
pub const TTYC_SETRGBB: tty_code_code = 209;
pub const TTYC_SETAL: tty_code_code = 208;
pub const TTYC_SETAF: tty_code_code = 207;
pub const TTYC_SETAB: tty_code_code = 206;
pub const TTYC_SE: tty_code_code = 205;
pub const TTYC_RMKX: tty_code_code = 204;
pub const TTYC_RMCUP: tty_code_code = 203;
pub const TTYC_RMACS: tty_code_code = 202;
pub const TTYC_RIN: tty_code_code = 201;
pub const TTYC_RI: tty_code_code = 200;
pub const TTYC_RGB: tty_code_code = 199;
pub const TTYC_REV: tty_code_code = 198;
pub const TTYC_RECT: tty_code_code = 197;
pub const TTYC_OP: tty_code_code = 196;
pub const TTYC_OL: tty_code_code = 195;
pub const TTYC_NOBR: tty_code_code = 194;
pub const TTYC_MS: tty_code_code = 193;
pub const TTYC_KUP7: tty_code_code = 192;
pub const TTYC_KUP6: tty_code_code = 191;
pub const TTYC_KUP5: tty_code_code = 190;
pub const TTYC_KUP4: tty_code_code = 189;
pub const TTYC_KUP3: tty_code_code = 188;
pub const TTYC_KUP2: tty_code_code = 187;
pub const TTYC_KRIT7: tty_code_code = 186;
pub const TTYC_KRIT6: tty_code_code = 185;
pub const TTYC_KRIT5: tty_code_code = 184;
pub const TTYC_KRIT4: tty_code_code = 183;
pub const TTYC_KRIT3: tty_code_code = 182;
pub const TTYC_KRIT2: tty_code_code = 181;
pub const TTYC_KRI: tty_code_code = 180;
pub const TTYC_KPRV7: tty_code_code = 179;
pub const TTYC_KPRV6: tty_code_code = 178;
pub const TTYC_KPRV5: tty_code_code = 177;
pub const TTYC_KPRV4: tty_code_code = 176;
pub const TTYC_KPRV3: tty_code_code = 175;
pub const TTYC_KPRV2: tty_code_code = 174;
pub const TTYC_KPP: tty_code_code = 173;
pub const TTYC_KNXT7: tty_code_code = 172;
pub const TTYC_KNXT6: tty_code_code = 171;
pub const TTYC_KNXT5: tty_code_code = 170;
pub const TTYC_KNXT4: tty_code_code = 169;
pub const TTYC_KNXT3: tty_code_code = 168;
pub const TTYC_KNXT2: tty_code_code = 167;
pub const TTYC_KNP: tty_code_code = 166;
pub const TTYC_KMOUS: tty_code_code = 165;
pub const TTYC_KLFT7: tty_code_code = 164;
pub const TTYC_KLFT6: tty_code_code = 163;
pub const TTYC_KLFT5: tty_code_code = 162;
pub const TTYC_KLFT4: tty_code_code = 161;
pub const TTYC_KLFT3: tty_code_code = 160;
pub const TTYC_KLFT2: tty_code_code = 159;
pub const TTYC_KIND: tty_code_code = 158;
pub const TTYC_KICH1: tty_code_code = 157;
pub const TTYC_KIC7: tty_code_code = 156;
pub const TTYC_KIC6: tty_code_code = 155;
pub const TTYC_KIC5: tty_code_code = 154;
pub const TTYC_KIC4: tty_code_code = 153;
pub const TTYC_KIC3: tty_code_code = 152;
pub const TTYC_KIC2: tty_code_code = 151;
pub const TTYC_KHOME: tty_code_code = 150;
pub const TTYC_KHOM7: tty_code_code = 149;
pub const TTYC_KHOM6: tty_code_code = 148;
pub const TTYC_KHOM5: tty_code_code = 147;
pub const TTYC_KHOM4: tty_code_code = 146;
pub const TTYC_KHOM3: tty_code_code = 145;
pub const TTYC_KHOM2: tty_code_code = 144;
pub const TTYC_KF9: tty_code_code = 143;
pub const TTYC_KF8: tty_code_code = 142;
pub const TTYC_KF7: tty_code_code = 141;
pub const TTYC_KF63: tty_code_code = 140;
pub const TTYC_KF62: tty_code_code = 139;
pub const TTYC_KF61: tty_code_code = 138;
pub const TTYC_KF60: tty_code_code = 137;
pub const TTYC_KF6: tty_code_code = 136;
pub const TTYC_KF59: tty_code_code = 135;
pub const TTYC_KF58: tty_code_code = 134;
pub const TTYC_KF57: tty_code_code = 133;
pub const TTYC_KF56: tty_code_code = 132;
pub const TTYC_KF55: tty_code_code = 131;
pub const TTYC_KF54: tty_code_code = 130;
pub const TTYC_KF53: tty_code_code = 129;
pub const TTYC_KF52: tty_code_code = 128;
pub const TTYC_KF51: tty_code_code = 127;
pub const TTYC_KF50: tty_code_code = 126;
pub const TTYC_KF5: tty_code_code = 125;
pub const TTYC_KF49: tty_code_code = 124;
pub const TTYC_KF48: tty_code_code = 123;
pub const TTYC_KF47: tty_code_code = 122;
pub const TTYC_KF46: tty_code_code = 121;
pub const TTYC_KF45: tty_code_code = 120;
pub const TTYC_KF44: tty_code_code = 119;
pub const TTYC_KF43: tty_code_code = 118;
pub const TTYC_KF42: tty_code_code = 117;
pub const TTYC_KF41: tty_code_code = 116;
pub const TTYC_KF40: tty_code_code = 115;
pub const TTYC_KF4: tty_code_code = 114;
pub const TTYC_KF39: tty_code_code = 113;
pub const TTYC_KF38: tty_code_code = 112;
pub const TTYC_KF37: tty_code_code = 111;
pub const TTYC_KF36: tty_code_code = 110;
pub const TTYC_KF35: tty_code_code = 109;
pub const TTYC_KF34: tty_code_code = 108;
pub const TTYC_KF33: tty_code_code = 107;
pub const TTYC_KF32: tty_code_code = 106;
pub const TTYC_KF31: tty_code_code = 105;
pub const TTYC_KF30: tty_code_code = 104;
pub const TTYC_KF3: tty_code_code = 103;
pub const TTYC_KF29: tty_code_code = 102;
pub const TTYC_KF28: tty_code_code = 101;
pub const TTYC_KF27: tty_code_code = 100;
pub const TTYC_KF26: tty_code_code = 99;
pub const TTYC_KF25: tty_code_code = 98;
pub const TTYC_KF24: tty_code_code = 97;
pub const TTYC_KF23: tty_code_code = 96;
pub const TTYC_KF22: tty_code_code = 95;
pub const TTYC_KF21: tty_code_code = 94;
pub const TTYC_KF20: tty_code_code = 93;
pub const TTYC_KF2: tty_code_code = 92;
pub const TTYC_KF19: tty_code_code = 91;
pub const TTYC_KF18: tty_code_code = 90;
pub const TTYC_KF17: tty_code_code = 89;
pub const TTYC_KF16: tty_code_code = 88;
pub const TTYC_KF15: tty_code_code = 87;
pub const TTYC_KF14: tty_code_code = 86;
pub const TTYC_KF13: tty_code_code = 85;
pub const TTYC_KF12: tty_code_code = 84;
pub const TTYC_KF11: tty_code_code = 83;
pub const TTYC_KF10: tty_code_code = 82;
pub const TTYC_KF1: tty_code_code = 81;
pub const TTYC_KEND7: tty_code_code = 80;
pub const TTYC_KEND6: tty_code_code = 79;
pub const TTYC_KEND5: tty_code_code = 78;
pub const TTYC_KEND4: tty_code_code = 77;
pub const TTYC_KEND3: tty_code_code = 76;
pub const TTYC_KEND2: tty_code_code = 75;
pub const TTYC_KEND: tty_code_code = 74;
pub const TTYC_KDN7: tty_code_code = 73;
pub const TTYC_KDN6: tty_code_code = 72;
pub const TTYC_KDN5: tty_code_code = 71;
pub const TTYC_KDN4: tty_code_code = 70;
pub const TTYC_KDN3: tty_code_code = 69;
pub const TTYC_KDN2: tty_code_code = 68;
pub const TTYC_KDCH1: tty_code_code = 67;
pub const TTYC_KDC7: tty_code_code = 66;
pub const TTYC_KDC6: tty_code_code = 65;
pub const TTYC_KDC5: tty_code_code = 64;
pub const TTYC_KDC4: tty_code_code = 63;
pub const TTYC_KDC3: tty_code_code = 62;
pub const TTYC_KDC2: tty_code_code = 61;
pub const TTYC_KCUU1: tty_code_code = 60;
pub const TTYC_KCUF1: tty_code_code = 59;
pub const TTYC_KCUD1: tty_code_code = 58;
pub const TTYC_KCUB1: tty_code_code = 57;
pub const TTYC_KCBT: tty_code_code = 56;
pub const TTYC_INVIS: tty_code_code = 55;
pub const TTYC_INDN: tty_code_code = 54;
pub const TTYC_IL1: tty_code_code = 53;
pub const TTYC_IL: tty_code_code = 52;
pub const TTYC_ICH1: tty_code_code = 51;
pub const TTYC_ICH: tty_code_code = 50;
pub const TTYC_HPA: tty_code_code = 49;
pub const TTYC_HOME: tty_code_code = 48;
pub const TTYC_HLS: tty_code_code = 47;
pub const TTYC_FSL: tty_code_code = 46;
pub const TTYC_ENMG: tty_code_code = 45;
pub const TTYC_ENFCS: tty_code_code = 44;
pub const TTYC_ENEKS: tty_code_code = 43;
pub const TTYC_ENBP: tty_code_code = 42;
pub const TTYC_ENACS: tty_code_code = 41;
pub const TTYC_EL1: tty_code_code = 40;
pub const TTYC_EL: tty_code_code = 39;
pub const TTYC_ED: tty_code_code = 38;
pub const TTYC_ECH: tty_code_code = 37;
pub const TTYC_E3: tty_code_code = 36;
pub const TTYC_DSMG: tty_code_code = 35;
pub const TTYC_DSFCS: tty_code_code = 34;
pub const TTYC_DSEKS: tty_code_code = 33;
pub const TTYC_DSBP: tty_code_code = 32;
pub const TTYC_DL1: tty_code_code = 31;
pub const TTYC_DL: tty_code_code = 30;
pub const TTYC_DIM: tty_code_code = 29;
pub const TTYC_DCH1: tty_code_code = 28;
pub const TTYC_DCH: tty_code_code = 27;
pub const TTYC_CVVIS: tty_code_code = 26;
pub const TTYC_CUU1: tty_code_code = 25;
pub const TTYC_CUU: tty_code_code = 24;
pub const TTYC_CUP: tty_code_code = 23;
pub const TTYC_CUF1: tty_code_code = 22;
pub const TTYC_CUF: tty_code_code = 21;
pub const TTYC_CUD1: tty_code_code = 20;
pub const TTYC_CUD: tty_code_code = 19;
pub const TTYC_CUB1: tty_code_code = 18;
pub const TTYC_CUB: tty_code_code = 17;
pub const TTYC_CSR: tty_code_code = 16;
pub const TTYC_CS: tty_code_code = 15;
pub const TTYC_CR: tty_code_code = 14;
pub const TTYC_COLORS: tty_code_code = 13;
pub const TTYC_CNORM: tty_code_code = 12;
pub const TTYC_CMG: tty_code_code = 11;
pub const TTYC_CLMG: tty_code_code = 10;
pub const TTYC_CLEAR: tty_code_code = 9;
pub const TTYC_CIVIS: tty_code_code = 8;
pub const TTYC_BOLD: tty_code_code = 7;
pub const TTYC_BLINK: tty_code_code = 6;
pub const TTYC_BIDI: tty_code_code = 5;
pub const TTYC_BEL: tty_code_code = 4;
pub const TTYC_BCE: tty_code_code = 3;
pub const TTYC_AX: tty_code_code = 2;
pub const TTYC_AM: tty_code_code = 1;
pub const TTYC_ACSC: tty_code_code = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct clients {
    pub tqh_first: *mut client,
    pub tqh_last: *mut *mut client,
}
static mut tty_log_fd: i32 = -(1 as i32);
#[no_mangle]
pub unsafe extern "C" fn tty_create_log() {
    let mut name: [i8; 64] = [0; 64];
    xsnprintf(
        name.as_mut_ptr(),
        ::core::mem::size_of::<[i8; 64]>() as u64,
        b"tmux-out-%ld.log\0" as *const u8 as *const i8,
        getpid() as i64,
    );
    tty_log_fd = open(
        name.as_mut_ptr(),
        0o1 as i32 | 0o100 as i32 | 0o1000 as i32,
        0o644 as i32,
    );
    if tty_log_fd != -(1 as i32)
        && fcntl(tty_log_fd, 2 as i32, 1 as i32) == -(1 as i32)
    {
        fatal(b"fcntl failed\0" as *const u8 as *const i8);
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_init(mut tty: *mut tty, mut c: *mut client) -> i32 {
    if isatty((*c).fd) == 0 {
        return -(1 as i32);
    }
    memset(
        tty as *mut libc::c_void,
        0 as i32,
        ::core::mem::size_of::<tty>() as u64,
    );
    (*tty).client = c;
    (*tty).cstyle = SCREEN_CURSOR_DEFAULT;
    (*tty).ccolour = -(1 as i32);
    (*tty).bg = -(1 as i32);
    (*tty).fg = (*tty).bg;
    if tcgetattr((*c).fd, &mut (*tty).tio) != 0 as i32 {
        return -(1 as i32);
    }
    return 0 as i32;
}
#[no_mangle]
pub unsafe extern "C" fn tty_resize(mut tty: *mut tty) {
    let mut c: *mut client = (*tty).client;
    let mut ws: winsize = winsize {
        ws_row: 0,
        ws_col: 0,
        ws_xpixel: 0,
        ws_ypixel: 0,
    };
    let mut sx: u_int = 0;
    let mut sy: u_int = 0;
    let mut xpixel: u_int = 0;
    let mut ypixel: u_int = 0;
    if ioctl(
        (*c).fd,
        0x5413 as i32 as u64,
        &mut ws as *mut winsize,
    ) != -(1 as i32)
    {
        sx = ws.ws_col as u_int;
        if sx == 0 as i32 as u32 {
            sx = 80 as i32 as u_int;
            xpixel = 0 as i32 as u_int;
        } else {
            xpixel = (ws.ws_xpixel as u32).wrapping_div(sx);
        }
        sy = ws.ws_row as u_int;
        if sy == 0 as i32 as u32 {
            sy = 24 as i32 as u_int;
            ypixel = 0 as i32 as u_int;
        } else {
            ypixel = (ws.ws_ypixel as u32).wrapping_div(sy);
        }
        if (xpixel == 0 as i32 as u32
            || ypixel == 0 as i32 as u32)
            && !((*tty).out).is_null()
            && (*tty).flags & 0x1000 as i32 == 0
            && (*(*tty).term).flags & 0x20 as i32 != 0
        {
            tty_puts(
                tty,
                b"\x1B[18t\x1B[14t\0" as *const u8 as *const i8,
            );
            (*tty).flags |= 0x1000 as i32;
        }
    } else {
        sx = 80 as i32 as u_int;
        sy = 24 as i32 as u_int;
        xpixel = 0 as i32 as u_int;
        ypixel = 0 as i32 as u_int;
    }
    log_debug(
        b"%s: %s now %ux%u (%ux%u)\0" as *const u8 as *const i8,
        (*::core::mem::transmute::<&[u8; 11], &[i8; 11]>(b"tty_resize\0")).as_ptr(),
        (*c).name,
        sx,
        sy,
        xpixel,
        ypixel,
    );
    tty_set_size(tty, sx, sy, xpixel, ypixel);
    tty_invalidate(tty);
}
#[no_mangle]
pub unsafe extern "C" fn tty_set_size(
    mut tty: *mut tty,
    mut sx: u_int,
    mut sy: u_int,
    mut xpixel: u_int,
    mut ypixel: u_int,
) {
    (*tty).sx = sx;
    (*tty).sy = sy;
    (*tty).xpixel = xpixel;
    (*tty).ypixel = ypixel;
}
unsafe extern "C" fn tty_read_callback(
    mut fd: i32,
    mut events: i16,
    mut data: *mut libc::c_void,
) {
    let mut tty: *mut tty = data as *mut tty;
    let mut c: *mut client = (*tty).client;
    let mut name: *const i8 = (*c).name;
    let mut size: size_t = evbuffer_get_length((*tty).in_0);
    let mut nread: i32 = 0;
    nread = evbuffer_read((*tty).in_0, (*c).fd, -(1 as i32));
    if nread == 0 as i32 || nread == -(1 as i32) {
        if nread == 0 as i32 {
            log_debug(
                b"%s: read closed\0" as *const u8 as *const i8,
                name,
            );
        } else {
            log_debug(
                b"%s: read error: %s\0" as *const u8 as *const i8,
                name,
                strerror(*__errno_location()),
            );
        }
        event_del(&mut (*tty).event_in);
        server_client_lost((*tty).client);
        return;
    }
    log_debug(
        b"%s: read %d bytes (already %zu)\0" as *const u8 as *const i8,
        name,
        nread,
        size,
    );
    while tty_keys_next(tty) != 0 {}
}
unsafe extern "C" fn tty_timer_callback(
    mut fd: i32,
    mut events: i16,
    mut data: *mut libc::c_void,
) {
    let mut tty: *mut tty = data as *mut tty;
    let mut c: *mut client = (*tty).client;
    let mut tv: timeval = {
        let mut init = timeval {
            tv_sec: 0,
            tv_usec: 100000 as i32 as __suseconds_t,
        };
        init
    };
    log_debug(
        b"%s: %zu discarded\0" as *const u8 as *const i8,
        (*c).name,
        (*tty).discarded,
    );
    (*c).flags = ((*c).flags as u64
        | ((0x8 as i32
            | 0x10 as i32
            | 0x1000000 as i32
            | 0x400 as i32
            | 0x2000000 as i32
            | 0x20000000 as i32) as u64
            | 0x4000000000 as u64)) as uint64_t;
    (*c).discarded =
        ((*c).discarded as u64).wrapping_add((*tty).discarded) as size_t as size_t;
    if (*tty).discarded
        < (1 as i32 as u32).wrapping_add(
            ((*tty).sx)
                .wrapping_mul((*tty).sy)
                .wrapping_div(8 as i32 as u32),
        ) as u64
    {
        (*tty).flags &= !(0x80 as i32);
        tty_invalidate(tty);
        return;
    }
    (*tty).discarded = 0 as i32 as size_t;
    event_add(&mut (*tty).timer, &mut tv);
}
unsafe extern "C" fn tty_block_maybe(mut tty: *mut tty) -> i32 {
    let mut c: *mut client = (*tty).client;
    let mut size: size_t = evbuffer_get_length((*tty).out);
    let mut tv: timeval = {
        let mut init = timeval {
            tv_sec: 0,
            tv_usec: 100000 as i32 as __suseconds_t,
        };
        init
    };
    if size == 0 as i32 as u64 {
        (*tty).flags &= !(0x8 as i32);
    } else if (*tty).flags & 0x8 as i32 != 0 {
        return 0 as i32;
    }
    if size
        < (1 as i32 as u32).wrapping_add(
            ((*tty).sx)
                .wrapping_mul((*tty).sy)
                .wrapping_mul(8 as i32 as u32),
        ) as u64
    {
        return 0 as i32;
    }
    if (*tty).flags & 0x80 as i32 != 0 {
        return 1 as i32;
    }
    (*tty).flags |= 0x80 as i32;
    log_debug(
        b"%s: can't keep up, %zu discarded\0" as *const u8 as *const i8,
        (*c).name,
        size,
    );
    evbuffer_drain((*tty).out, size);
    (*c).discarded = ((*c).discarded as u64).wrapping_add(size) as size_t as size_t;
    (*tty).discarded = 0 as i32 as size_t;
    event_add(&mut (*tty).timer, &mut tv);
    return 1 as i32;
}
unsafe extern "C" fn tty_write_callback(
    mut fd: i32,
    mut events: i16,
    mut data: *mut libc::c_void,
) {
    let mut tty: *mut tty = data as *mut tty;
    let mut c: *mut client = (*tty).client;
    let mut size: size_t = evbuffer_get_length((*tty).out);
    let mut nwrite: i32 = 0;
    nwrite = evbuffer_write((*tty).out, (*c).fd);
    if nwrite == -(1 as i32) {
        return;
    }
    log_debug(
        b"%s: wrote %d bytes (of %zu)\0" as *const u8 as *const i8,
        (*c).name,
        nwrite,
        size,
    );
    if (*c).redraw > 0 as i32 as u64 {
        if nwrite as size_t >= (*c).redraw {
            (*c).redraw = 0 as i32 as size_t;
        } else {
            (*c).redraw = ((*c).redraw as u64).wrapping_sub(nwrite as u64)
                as size_t as size_t;
        }
        log_debug(
            b"%s: waiting for redraw, %zu bytes left\0" as *const u8 as *const i8,
            (*c).name,
            (*c).redraw,
        );
    } else if tty_block_maybe(tty) != 0 {
        return;
    }
    if evbuffer_get_length((*tty).out) != 0 as i32 as u64 {
        event_add(&mut (*tty).event_out, std::ptr::null::<timeval>());
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_open(
    mut tty: *mut tty,
    mut cause: *mut *mut i8,
) -> i32 {
    let mut c: *mut client = (*tty).client;
    (*tty).term = tty_term_create(
        tty,
        (*c).term_name,
        (*c).term_caps,
        (*c).term_ncaps,
        &mut (*c).term_features,
        cause,
    );
    if ((*tty).term).is_null() {
        tty_close(tty);
        return -(1 as i32);
    }
    (*tty).flags |= 0x20 as i32;
    (*tty).flags &=
        !(0x1 as i32 | 0x2 as i32 | 0x80 as i32 | 0x4 as i32);
    event_set(
        &mut (*tty).event_in,
        (*c).fd,
        (0x10 as i32 | 0x2 as i32) as i16,
        Some(
            tty_read_callback
                as unsafe extern "C" fn(i32, i16, *mut libc::c_void) -> (),
        ),
        tty as *mut libc::c_void,
    );
    (*tty).in_0 = evbuffer_new();
    if ((*tty).in_0).is_null() {
        fatal(b"out of memory\0" as *const u8 as *const i8);
    }
    event_set(
        &mut (*tty).event_out,
        (*c).fd,
        0x4 as i32 as i16,
        Some(
            tty_write_callback
                as unsafe extern "C" fn(i32, i16, *mut libc::c_void) -> (),
        ),
        tty as *mut libc::c_void,
    );
    (*tty).out = evbuffer_new();
    if ((*tty).out).is_null() {
        fatal(b"out of memory\0" as *const u8 as *const i8);
    }
    event_set(
        &mut (*tty).timer,
        -(1 as i32),
        0 as i32 as i16,
        Some(
            tty_timer_callback
                as unsafe extern "C" fn(i32, i16, *mut libc::c_void) -> (),
        ),
        tty as *mut libc::c_void,
    );
    tty_start_tty(tty);
    tty_keys_build(tty);
    return 0 as i32;
}
unsafe extern "C" fn tty_start_timer_callback(
    mut fd: i32,
    mut events: i16,
    mut data: *mut libc::c_void,
) {
    let mut tty: *mut tty = data as *mut tty;
    let mut c: *mut client = (*tty).client;
    log_debug(
        b"%s: start timer fired\0" as *const u8 as *const i8,
        (*c).name,
    );
    if (*tty).flags & (0x100 as i32 | 0x800 as i32 | 0x200 as i32)
        == 0 as i32
    {
        tty_update_features(tty);
    }
    (*tty).flags |= 0x100 as i32 | 0x800 as i32 | 0x200 as i32;
}
#[no_mangle]
pub unsafe extern "C" fn tty_start_tty(mut tty: *mut tty) {
    let mut c: *mut client = (*tty).client;
    let mut tio: termios = termios {
        c_iflag: 0,
        c_oflag: 0,
        c_cflag: 0,
        c_lflag: 0,
        c_line: 0,
        c_cc: [0; 32],
        c_ispeed: 0,
        c_ospeed: 0,
    };
    let mut tv: timeval = {
        let mut init = timeval {
            tv_sec: 5 as i32 as __time_t,
            tv_usec: 0,
        };
        init
    };
    setblocking((*c).fd, 0 as i32);
    event_add(&mut (*tty).event_in, std::ptr::null::<timeval>());
    memcpy(
        &mut tio as *mut termios as *mut libc::c_void,
        &mut (*tty).tio as *mut termios as *const libc::c_void,
        ::core::mem::size_of::<termios>() as u64,
    );
    tio.c_iflag &= !(0o2000 as i32
        | 0o10000 as i32
        | 0o400 as i32
        | 0o100 as i32
        | 0o200 as i32
        | 0o20000 as i32
        | 0o40 as i32) as u32;
    tio.c_iflag |= 0o1 as i32 as u32;
    tio.c_oflag &=
        !(0o1 as i32 | 0o4 as i32 | 0o10 as i32 | 0o40 as i32)
            as u32;
    tio.c_lflag &= !(0o100000 as i32
        | 0o2 as i32
        | 0o10 as i32
        | 0o20 as i32
        | 0o100 as i32
        | 0o1000 as i32
        | 0o2000 as i32
        | 0o4000 as i32
        | 0o1 as i32) as u32;
    tio.c_cc[6 as i32 as usize] = 1 as i32 as cc_t;
    tio.c_cc[5 as i32 as usize] = 0 as i32 as cc_t;
    if tcsetattr((*c).fd, 0 as i32, &mut tio) == 0 as i32 {
        tcflush((*c).fd, 1 as i32);
    }
    tty_putcode(tty, TTYC_SMCUP);
    tty_putcode(tty, TTYC_SMKX);
    tty_putcode(tty, TTYC_CLEAR);
    if tty_acs_needed(tty) != 0 {
        log_debug(
            b"%s: using capabilities for ACS\0" as *const u8 as *const i8,
            (*c).name,
        );
        tty_putcode(tty, TTYC_ENACS);
    } else {
        log_debug(
            b"%s: using UTF-8 for ACS\0" as *const u8 as *const i8,
            (*c).name,
        );
    }
    tty_putcode(tty, TTYC_CNORM);
    if tty_term_has((*tty).term, TTYC_KMOUS) != 0 {
        tty_puts(
            tty,
            b"\x1B[?1000l\x1B[?1002l\x1B[?1003l\0" as *const u8 as *const i8,
        );
        tty_puts(
            tty,
            b"\x1B[?1006l\x1B[?1005l\0" as *const u8 as *const i8,
        );
    }
    if tty_term_has((*tty).term, TTYC_ENBP) != 0 {
        tty_putcode(tty, TTYC_ENBP);
    }
    event_set(
        &mut (*tty).start_timer,
        -(1 as i32),
        0 as i32 as i16,
        Some(
            tty_start_timer_callback
                as unsafe extern "C" fn(i32, i16, *mut libc::c_void) -> (),
        ),
        tty as *mut libc::c_void,
    );
    event_add(&mut (*tty).start_timer, &mut tv);
    (*tty).flags |= 0x10 as i32;
    tty_invalidate(tty);
    if (*tty).ccolour != -(1 as i32) {
        tty_force_cursor_colour(tty, -(1 as i32));
    }
    (*tty).mouse_drag_flag = 0 as i32;
    (*tty).mouse_drag_update = None;
    (*tty).mouse_drag_release = None;
}
#[no_mangle]
pub unsafe extern "C" fn tty_send_requests(mut tty: *mut tty) {
    if !(*tty).flags & 0x10 as i32 != 0 {
        return;
    }
    if (*(*tty).term).flags & 0x20 as i32 != 0 {
        if !(*(*tty).term).flags & 0x100 as i32 != 0 {
            tty_puts(tty, b"\x1B[c\0" as *const u8 as *const i8);
        }
        if !(*tty).flags & 0x800 as i32 != 0 {
            tty_puts(tty, b"\x1B[>c\0" as *const u8 as *const i8);
        }
        if !(*tty).flags & 0x200 as i32 != 0 {
            tty_puts(tty, b"\x1B[>q\0" as *const u8 as *const i8);
        }
        tty_puts(
            tty,
            b"\x1B]10;?\x1B\\\0" as *const u8 as *const i8,
        );
        tty_puts(
            tty,
            b"\x1B]11;?\x1B\\\0" as *const u8 as *const i8,
        );
    } else {
        (*tty).flags |= 0x100 as i32 | 0x800 as i32 | 0x200 as i32;
    }
    (*tty).last_requests = time(std::ptr::null_mut::<time_t>());
}
#[no_mangle]
pub unsafe extern "C" fn tty_repeat_requests(mut tty: *mut tty) {
    let mut t: time_t = time(std::ptr::null_mut::<time_t>());
    if !(*tty).flags & 0x10 as i32 != 0 {
        return;
    }
    if t - (*tty).last_requests <= 30 as i32 as i64 {
        return;
    }
    (*tty).last_requests = t;
    if (*(*tty).term).flags & 0x20 as i32 != 0 {
        tty_puts(
            tty,
            b"\x1B]10;?\x1B\\\0" as *const u8 as *const i8,
        );
        tty_puts(
            tty,
            b"\x1B]11;?\x1B\\\0" as *const u8 as *const i8,
        );
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_stop_tty(mut tty: *mut tty) {
    let mut c: *mut client = (*tty).client;
    let mut ws: winsize = winsize {
        ws_row: 0,
        ws_col: 0,
        ws_xpixel: 0,
        ws_ypixel: 0,
    };
    if (*tty).flags & 0x10 as i32 == 0 {
        return;
    }
    (*tty).flags &= !(0x10 as i32);
    event_del(&mut (*tty).start_timer);
    event_del(&mut (*tty).timer);
    (*tty).flags &= !(0x80 as i32);
    event_del(&mut (*tty).event_in);
    event_del(&mut (*tty).event_out);
    if ioctl(
        (*c).fd,
        0x5413 as i32 as u64,
        &mut ws as *mut winsize,
    ) == -(1 as i32)
    {
        return;
    }
    if tcsetattr((*c).fd, 0 as i32, &mut (*tty).tio) == -(1 as i32) {
        return;
    }
    tty_raw(
        tty,
        tty_term_string_ii(
            (*tty).term,
            TTYC_CSR,
            0 as i32,
            ws.ws_row as i32 - 1 as i32,
        ),
    );
    if tty_acs_needed(tty) != 0 {
        tty_raw(tty, tty_term_string((*tty).term, TTYC_RMACS));
    }
    tty_raw(tty, tty_term_string((*tty).term, TTYC_SGR0));
    tty_raw(tty, tty_term_string((*tty).term, TTYC_RMKX));
    tty_raw(tty, tty_term_string((*tty).term, TTYC_CLEAR));
    if (*tty).cstyle as u32 != SCREEN_CURSOR_DEFAULT as i32 as u32 {
        if tty_term_has((*tty).term, TTYC_SE) != 0 {
            tty_raw(tty, tty_term_string((*tty).term, TTYC_SE));
        } else if tty_term_has((*tty).term, TTYC_SS) != 0 {
            tty_raw(
                tty,
                tty_term_string_i((*tty).term, TTYC_SS, 0 as i32),
            );
        }
    }
    if (*tty).ccolour != -(1 as i32) {
        tty_raw(tty, tty_term_string((*tty).term, TTYC_CR));
    }
    tty_raw(tty, tty_term_string((*tty).term, TTYC_CNORM));
    if tty_term_has((*tty).term, TTYC_KMOUS) != 0 {
        tty_raw(
            tty,
            b"\x1B[?1000l\x1B[?1002l\x1B[?1003l\0" as *const u8 as *const i8,
        );
        tty_raw(
            tty,
            b"\x1B[?1006l\x1B[?1005l\0" as *const u8 as *const i8,
        );
    }
    if tty_term_has((*tty).term, TTYC_DSBP) != 0 {
        tty_raw(tty, tty_term_string((*tty).term, TTYC_DSBP));
    }
    if (*(*tty).term).flags & 0x20 as i32 != 0 {
        tty_raw(tty, b"\x1B[?7727l\0" as *const u8 as *const i8);
    }
    tty_raw(tty, tty_term_string((*tty).term, TTYC_DSFCS));
    tty_raw(tty, tty_term_string((*tty).term, TTYC_DSEKS));
    if (*(*tty).term).flags & 0x4 as i32 != 0 {
        tty_raw(tty, tty_term_string((*tty).term, TTYC_DSMG));
    }
    tty_raw(tty, tty_term_string((*tty).term, TTYC_RMCUP));
    setblocking((*c).fd, 1 as i32);
}
#[no_mangle]
pub unsafe extern "C" fn tty_close(mut tty: *mut tty) {
    if event_initialized(&mut (*tty).key_timer) != 0 {
        event_del(&mut (*tty).key_timer);
    }
    tty_stop_tty(tty);
    if (*tty).flags & 0x20 as i32 != 0 {
        evbuffer_free((*tty).in_0);
        event_del(&mut (*tty).event_in);
        evbuffer_free((*tty).out);
        event_del(&mut (*tty).event_out);
        tty_term_free((*tty).term);
        tty_keys_free(tty);
        (*tty).flags &= !(0x20 as i32);
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_free(mut tty: *mut tty) {
    tty_close(tty);
}
#[no_mangle]
pub unsafe extern "C" fn tty_update_features(mut tty: *mut tty) {
    let mut c: *mut client = (*tty).client;
    if tty_apply_features((*tty).term, (*c).term_features) != 0 {
        tty_term_apply_overrides((*tty).term);
    }
    if (*(*tty).term).flags & 0x4 as i32 != 0 {
        tty_putcode(tty, TTYC_ENMG);
    }
    if options_get_number(
        global_options,
        b"extended-keys\0" as *const u8 as *const i8,
    ) != 0
    {
        tty_puts(tty, tty_term_string((*tty).term, TTYC_ENEKS));
    }
    if options_get_number(
        global_options,
        b"focus-events\0" as *const u8 as *const i8,
    ) != 0
    {
        tty_puts(tty, tty_term_string((*tty).term, TTYC_ENFCS));
    }
    if (*(*tty).term).flags & 0x20 as i32 != 0 {
        tty_puts(tty, b"\x1B[?7727h\0" as *const u8 as *const i8);
    }
    server_redraw_client(c);
    tty_invalidate(tty);
}
#[no_mangle]
pub unsafe extern "C" fn tty_raw(mut tty: *mut tty, mut s: *const i8) {
    let mut c: *mut client = (*tty).client;
    let mut n: ssize_t = 0;
    let mut slen: ssize_t = 0;
    let mut i: u_int = 0;
    slen = strlen(s) as ssize_t;
    i = 0 as i32 as u_int;
    while i < 5 as i32 as u32 {
        n = write((*c).fd, s as *const libc::c_void, slen as size_t);
        if n >= 0 as i32 as i64 {
            s = s.offset(n as isize);
            slen -= n;
            if slen == 0 as i32 as i64 {
                break;
            }
        } else if n == -(1 as i32) as i64
            && *__errno_location() != 11 as i32
        {
            break;
        }
        usleep(100 as i32 as __useconds_t);
        i = i.wrapping_add(1);
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_putcode(mut tty: *mut tty, mut code: tty_code_code) {
    tty_puts(tty, tty_term_string((*tty).term, code));
}
#[no_mangle]
pub unsafe extern "C" fn tty_putcode_i(
    mut tty: *mut tty,
    mut code: tty_code_code,
    mut a: i32,
) {
    if a < 0 as i32 {
        return;
    }
    tty_puts(tty, tty_term_string_i((*tty).term, code, a));
}
#[no_mangle]
pub unsafe extern "C" fn tty_putcode_ii(
    mut tty: *mut tty,
    mut code: tty_code_code,
    mut a: i32,
    mut b: i32,
) {
    if a < 0 as i32 || b < 0 as i32 {
        return;
    }
    tty_puts(tty, tty_term_string_ii((*tty).term, code, a, b));
}
#[no_mangle]
pub unsafe extern "C" fn tty_putcode_iii(
    mut tty: *mut tty,
    mut code: tty_code_code,
    mut a: i32,
    mut b: i32,
    mut c: i32,
) {
    if a < 0 as i32 || b < 0 as i32 || c < 0 as i32 {
        return;
    }
    tty_puts(tty, tty_term_string_iii((*tty).term, code, a, b, c));
}
#[no_mangle]
pub unsafe extern "C" fn tty_putcode_s(
    mut tty: *mut tty,
    mut code: tty_code_code,
    mut a: *const i8,
) {
    if !a.is_null() {
        tty_puts(tty, tty_term_string_s((*tty).term, code, a));
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_putcode_ss(
    mut tty: *mut tty,
    mut code: tty_code_code,
    mut a: *const i8,
    mut b: *const i8,
) {
    if !a.is_null() && !b.is_null() {
        tty_puts(tty, tty_term_string_ss((*tty).term, code, a, b));
    }
}
unsafe extern "C" fn tty_add(mut tty: *mut tty, mut buf: *const i8, mut len: size_t) {
    let mut c: *mut client = (*tty).client;
    if (*tty).flags & 0x80 as i32 != 0 {
        (*tty).discarded =
            ((*tty).discarded as u64).wrapping_add(len) as size_t as size_t;
        return;
    }
    evbuffer_add((*tty).out, buf as *const libc::c_void, len);
    log_debug(
        b"%s: %.*s\0" as *const u8 as *const i8,
        (*c).name,
        len as i32,
        buf,
    );
    (*c).written = ((*c).written as u64).wrapping_add(len) as size_t as size_t;
    if tty_log_fd != -(1 as i32) {
        write(tty_log_fd, buf as *const libc::c_void, len);
    }
    if (*tty).flags & 0x10 as i32 != 0 {
        event_add(&mut (*tty).event_out, std::ptr::null::<timeval>());
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_puts(mut tty: *mut tty, mut s: *const i8) {
    if *s as i32 != '\0' as i32 {
        tty_add(tty, s, strlen(s));
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_putc(mut tty: *mut tty, mut ch: u_char) {
    let mut acs: *const i8 = std::ptr::null::<i8>();
    if (*(*tty).term).flags & 0x2 as i32 != 0
        && ch as i32 >= 0x20 as i32
        && ch as i32 != 0x7f as i32
        && (*tty).cy == ((*tty).sy).wrapping_sub(1 as i32 as u32)
        && ((*tty).cx).wrapping_add(1 as i32 as u32) >= (*tty).sx
    {
        return;
    }
    if (*tty).cell.attr as i32 & 0x80 as i32 != 0 {
        acs = tty_acs_get(tty, ch);
        if !acs.is_null() {
            tty_add(tty, acs, strlen(acs));
        } else {
            tty_add(
                tty,
                &mut ch as *mut u_char as *const i8,
                1 as i32 as size_t,
            );
        }
    } else {
        tty_add(
            tty,
            &mut ch as *mut u_char as *const i8,
            1 as i32 as size_t,
        );
    }
    if ch as i32 >= 0x20 as i32 && ch as i32 != 0x7f as i32 {
        if (*tty).cx >= (*tty).sx {
            (*tty).cx = 1 as i32 as u_int;
            if (*tty).cy != (*tty).rlower {
                (*tty).cy = ((*tty).cy).wrapping_add(1);
            }
            if (*(*tty).term).flags & 0x2 as i32 != 0 {
                tty_putcode_ii(
                    tty,
                    TTYC_CUP,
                    (*tty).cy as i32,
                    (*tty).cx as i32,
                );
            }
        } else {
            (*tty).cx = ((*tty).cx).wrapping_add(1);
        }
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_putn(
    mut tty: *mut tty,
    mut buf: *const libc::c_void,
    mut len: size_t,
    mut width: u_int,
) {
    if (*(*tty).term).flags & 0x2 as i32 != 0
        && (*tty).cy == ((*tty).sy).wrapping_sub(1 as i32 as u32)
        && ((*tty).cx as u64).wrapping_add(len) >= (*tty).sx as u64
    {
        len = ((*tty).sx)
            .wrapping_sub((*tty).cx)
            .wrapping_sub(1 as i32 as u32) as size_t;
    }
    tty_add(tty, buf as *const i8, len);
    if ((*tty).cx).wrapping_add(width) > (*tty).sx {
        (*tty).cx = ((*tty).cx).wrapping_add(width).wrapping_sub((*tty).sx);
        if (*tty).cx <= (*tty).sx {
            (*tty).cy = ((*tty).cy).wrapping_add(1);
        } else {
            (*tty).cy = (2147483647 as i32 as u32)
                .wrapping_mul(2 as u32)
                .wrapping_add(1 as u32);
            (*tty).cx = (*tty).cy;
        }
    } else {
        (*tty).cx = ((*tty).cx as u32).wrapping_add(width) as u_int as u_int;
    };
}
unsafe extern "C" fn tty_set_italics(mut tty: *mut tty) {
    let mut s: *const i8 = std::ptr::null::<i8>();
    if tty_term_has((*tty).term, TTYC_SITM) != 0 {
        s = options_get_string(
            global_options,
            b"default-terminal\0" as *const u8 as *const i8,
        );
        if strcmp(s, b"screen\0" as *const u8 as *const i8) != 0 as i32
            && strncmp(
                s,
                b"screen-\0" as *const u8 as *const i8,
                7 as i32 as u64,
            ) != 0 as i32
        {
            tty_putcode(tty, TTYC_SITM);
            return;
        }
    }
    tty_putcode(tty, TTYC_SMSO);
}
#[no_mangle]
pub unsafe extern "C" fn tty_set_title(mut tty: *mut tty, mut title: *const i8) {
    if tty_term_has((*tty).term, TTYC_TSL) == 0 || tty_term_has((*tty).term, TTYC_FSL) == 0 {
        return;
    }
    tty_putcode(tty, TTYC_TSL);
    tty_puts(tty, title);
    tty_putcode(tty, TTYC_FSL);
}
#[no_mangle]
pub unsafe extern "C" fn tty_set_path(mut tty: *mut tty, mut title: *const i8) {
    if tty_term_has((*tty).term, TTYC_SWD) == 0 || tty_term_has((*tty).term, TTYC_FSL) == 0 {
        return;
    }
    tty_putcode(tty, TTYC_SWD);
    tty_puts(tty, title);
    tty_putcode(tty, TTYC_FSL);
}
unsafe extern "C" fn tty_force_cursor_colour(mut tty: *mut tty, mut c: i32) {
    let mut r: u_char = 0;
    let mut g: u_char = 0;
    let mut b: u_char = 0;
    let mut s: [i8; 13] = [0; 13];
    if c != -(1 as i32) {
        c = colour_force_rgb(c);
    }
    if c == (*tty).ccolour {
        return;
    }
    if c == -(1 as i32) {
        tty_putcode(tty, TTYC_CR);
    } else {
        colour_split_rgb(c, &mut r, &mut g, &mut b);
        xsnprintf(
            s.as_mut_ptr(),
            ::core::mem::size_of::<[i8; 13]>() as u64,
            b"rgb:%02hhx/%02hhx/%02hhx\0" as *const u8 as *const i8,
            r as i32,
            g as i32,
            b as i32,
        );
        tty_putcode_s(tty, TTYC_CS, s.as_mut_ptr());
    }
    (*tty).ccolour = c;
}
unsafe extern "C" fn tty_update_cursor(
    mut tty: *mut tty,
    mut mode: i32,
    mut s: *mut screen,
) -> i32 {
    let mut cstyle: screen_cursor_style = SCREEN_CURSOR_DEFAULT;
    let mut ccolour: i32 = 0;
    let mut changed: i32 = 0;
    let mut cmode: i32 = mode;
    if !s.is_null() {
        ccolour = (*s).ccolour;
        if (*s).ccolour == -(1 as i32) {
            ccolour = (*s).default_ccolour;
        }
        tty_force_cursor_colour(tty, ccolour);
    }
    if !cmode & 0x1 as i32 != 0 {
        if (*tty).mode & 0x1 as i32 != 0 {
            tty_putcode(tty, TTYC_CIVIS);
        }
        return cmode;
    }
    if s.is_null() {
        cstyle = (*tty).cstyle;
    } else {
        cstyle = (*s).cstyle;
        if cstyle as u32 == SCREEN_CURSOR_DEFAULT as i32 as u32 {
            if !cmode & 0x20000 as i32 != 0 {
                if (*s).default_mode & 0x80 as i32 != 0 {
                    cmode |= 0x80 as i32;
                } else {
                    cmode &= !(0x80 as i32);
                }
            }
            cstyle = (*s).default_cstyle;
        }
    }
    changed = cmode ^ (*tty).mode;
    if changed & (0x1 as i32 | 0x80 as i32 | 0x10000 as i32)
        == 0 as i32
        && cstyle as u32 == (*tty).cstyle as u32
    {
        return cmode;
    }
    tty_putcode(tty, TTYC_CNORM);
    match cstyle as u32 {
        0 => {
            if (*tty).cstyle as u32 != SCREEN_CURSOR_DEFAULT as i32 as u32
            {
                if tty_term_has((*tty).term, TTYC_SE) != 0 {
                    tty_putcode(tty, TTYC_SE);
                } else {
                    tty_putcode_i(tty, TTYC_SS, 0 as i32);
                }
            }
            if cmode & (0x80 as i32 | 0x10000 as i32) != 0 {
                tty_putcode(tty, TTYC_CVVIS);
            }
        }
        1 => {
            if tty_term_has((*tty).term, TTYC_SS) != 0 {
                if cmode & 0x80 as i32 != 0 {
                    tty_putcode_i(tty, TTYC_SS, 1 as i32);
                } else {
                    tty_putcode_i(tty, TTYC_SS, 2 as i32);
                }
            } else if cmode & 0x80 as i32 != 0 {
                tty_putcode(tty, TTYC_CVVIS);
            }
        }
        2 => {
            if tty_term_has((*tty).term, TTYC_SS) != 0 {
                if cmode & 0x80 as i32 != 0 {
                    tty_putcode_i(tty, TTYC_SS, 3 as i32);
                } else {
                    tty_putcode_i(tty, TTYC_SS, 4 as i32);
                }
            } else if cmode & 0x80 as i32 != 0 {
                tty_putcode(tty, TTYC_CVVIS);
            }
        }
        3 => {
            if tty_term_has((*tty).term, TTYC_SS) != 0 {
                if cmode & 0x80 as i32 != 0 {
                    tty_putcode_i(tty, TTYC_SS, 5 as i32);
                } else {
                    tty_putcode_i(tty, TTYC_SS, 6 as i32);
                }
            } else if cmode & 0x80 as i32 != 0 {
                tty_putcode(tty, TTYC_CVVIS);
            }
        }
        _ => {}
    }
    (*tty).cstyle = cstyle;
    return cmode;
}
#[no_mangle]
pub unsafe extern "C" fn tty_update_mode(
    mut tty: *mut tty,
    mut mode: i32,
    mut s: *mut screen,
) {
    let mut term: *mut tty_term = (*tty).term;
    let mut c: *mut client = (*tty).client;
    let mut changed: i32 = 0;
    if (*tty).flags & 0x1 as i32 != 0 {
        mode &= !(0x1 as i32);
    }
    if tty_update_cursor(tty, mode, s) & 0x80 as i32 != 0 {
        mode |= 0x80 as i32;
    } else {
        mode &= !(0x80 as i32);
    }
    changed = mode ^ (*tty).mode;
    if log_get_level() != 0 as i32 && changed != 0 as i32 {
        log_debug(
            b"%s: current mode %s\0" as *const u8 as *const i8,
            (*c).name,
            screen_mode_to_string((*tty).mode),
        );
        log_debug(
            b"%s: setting mode %s\0" as *const u8 as *const i8,
            (*c).name,
            screen_mode_to_string(mode),
        );
    }
    if changed & (0x20 as i32 | 0x40 as i32 | 0x1000 as i32) != 0
        && tty_term_has(term, TTYC_KMOUS) != 0
    {
        tty_puts(
            tty,
            b"\x1B[?1006l\x1B[?1000l\x1B[?1002l\x1B[?1003l\0" as *const u8 as *const i8,
        );
        if mode & (0x20 as i32 | 0x40 as i32 | 0x1000 as i32) != 0 {
            tty_puts(tty, b"\x1B[?1006h\0" as *const u8 as *const i8);
        }
        if mode & 0x1000 as i32 != 0 {
            tty_puts(
                tty,
                b"\x1B[?1000h\x1B[?1002h\x1B[?1003h\0" as *const u8 as *const i8,
            );
        } else if mode & 0x40 as i32 != 0 {
            tty_puts(
                tty,
                b"\x1B[?1000h\x1B[?1002h\0" as *const u8 as *const i8,
            );
        } else if mode & 0x20 as i32 != 0 {
            tty_puts(tty, b"\x1B[?1000h\0" as *const u8 as *const i8);
        }
    }
    (*tty).mode = mode;
}
unsafe extern "C" fn tty_emulate_repeat(
    mut tty: *mut tty,
    mut code: tty_code_code,
    mut code1: tty_code_code,
    mut n: u_int,
) {
    if tty_term_has((*tty).term, code) != 0 {
        tty_putcode_i(tty, code, n as i32);
    } else {
        loop {
            let fresh0 = n;
            n = n.wrapping_sub(1);
            if fresh0 <= 0 as i32 as u32 {
                break;
            }
            tty_putcode(tty, code1);
        }
    };
}
unsafe extern "C" fn tty_repeat_space(mut tty: *mut tty, mut n: u_int) {
    static mut s: [i8; 500] = [0; 500];
    if *s.as_mut_ptr() as i32 != ' ' as i32 {
        memset(
            s.as_mut_ptr() as *mut libc::c_void,
            ' ' as i32,
            ::core::mem::size_of::<[i8; 500]>() as u64,
        );
    }
    while n as u64 > ::core::mem::size_of::<[i8; 500]>() as u64 {
        tty_putn(
            tty,
            s.as_mut_ptr() as *const libc::c_void,
            ::core::mem::size_of::<[i8; 500]>() as u64,
            ::core::mem::size_of::<[i8; 500]>() as u64 as u_int,
        );
        n = (n as u64)
            .wrapping_sub(::core::mem::size_of::<[i8; 500]>() as u64)
            as u_int as u_int;
    }
    if n != 0 as i32 as u32 {
        tty_putn(tty, s.as_mut_ptr() as *const libc::c_void, n as size_t, n);
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_window_bigger(mut tty: *mut tty) -> i32 {
    let mut c: *mut client = (*tty).client;
    let mut w: *mut window = (*(*(*c).session).curw).window;
    return ((*tty).sx < (*w).sx || ((*tty).sy).wrapping_sub(status_line_size(c)) < (*w).sy)
        as i32;
}
#[no_mangle]
pub unsafe extern "C" fn tty_window_offset(
    mut tty: *mut tty,
    mut ox: *mut u_int,
    mut oy: *mut u_int,
    mut sx: *mut u_int,
    mut sy: *mut u_int,
) -> i32 {
    *ox = (*tty).oox;
    *oy = (*tty).ooy;
    *sx = (*tty).osx;
    *sy = (*tty).osy;
    return (*tty).oflag;
}
unsafe extern "C" fn tty_window_offset1(
    mut tty: *mut tty,
    mut ox: *mut u_int,
    mut oy: *mut u_int,
    mut sx: *mut u_int,
    mut sy: *mut u_int,
) -> i32 {
    let mut c: *mut client = (*tty).client;
    let mut w: *mut window = (*(*(*c).session).curw).window;
    let mut wp: *mut window_pane = server_client_get_pane(c);
    let mut cx: u_int = 0;
    let mut cy: u_int = 0;
    let mut lines: u_int = 0;
    lines = status_line_size(c);
    if (*tty).sx >= (*w).sx && ((*tty).sy).wrapping_sub(lines) >= (*w).sy {
        *ox = 0 as i32 as u_int;
        *oy = 0 as i32 as u_int;
        *sx = (*w).sx;
        *sy = (*w).sy;
        (*c).pan_window = std::ptr::null_mut::<libc::c_void>();
        return 0 as i32;
    }
    *sx = (*tty).sx;
    *sy = ((*tty).sy).wrapping_sub(lines);
    if (*c).pan_window == w as *mut libc::c_void {
        if *sx >= (*w).sx {
            (*c).pan_ox = 0 as i32 as u_int;
        } else if ((*c).pan_ox).wrapping_add(*sx) > (*w).sx {
            (*c).pan_ox = ((*w).sx).wrapping_sub(*sx);
        }
        *ox = (*c).pan_ox;
        if *sy >= (*w).sy {
            (*c).pan_oy = 0 as i32 as u_int;
        } else if ((*c).pan_oy).wrapping_add(*sy) > (*w).sy {
            (*c).pan_oy = ((*w).sy).wrapping_sub(*sy);
        }
        *oy = (*c).pan_oy;
        return 1 as i32;
    }
    if !(*(*wp).screen).mode & 0x1 as i32 != 0 {
        *ox = 0 as i32 as u_int;
        *oy = 0 as i32 as u_int;
    } else {
        cx = ((*wp).xoff).wrapping_add((*(*wp).screen).cx);
        cy = ((*wp).yoff).wrapping_add((*(*wp).screen).cy);
        if cx < *sx {
            *ox = 0 as i32 as u_int;
        } else if cx > ((*w).sx).wrapping_sub(*sx) {
            *ox = ((*w).sx).wrapping_sub(*sx);
        } else {
            *ox = cx.wrapping_sub((*sx).wrapping_div(2 as i32 as u32));
        }
        if cy < *sy {
            *oy = 0 as i32 as u_int;
        } else if cy > ((*w).sy).wrapping_sub(*sy) {
            *oy = ((*w).sy).wrapping_sub(*sy);
        } else {
            *oy = cy.wrapping_sub((*sy).wrapping_div(2 as i32 as u32));
        }
    }
    (*c).pan_window = std::ptr::null_mut::<libc::c_void>();
    return 1 as i32;
}
#[no_mangle]
pub unsafe extern "C" fn tty_update_window_offset(mut w: *mut window) {
    let mut c: *mut client = std::ptr::null_mut::<client>();
    c = clients.tqh_first;
    while !c.is_null() {
        if !((*c).session).is_null()
            && !((*(*c).session).curw).is_null()
            && (*(*(*c).session).curw).window == w
        {
            tty_update_client_offset(c);
        }
        c = (*c).entry.tqe_next;
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_update_client_offset(mut c: *mut client) {
    let mut ox: u_int = 0;
    let mut oy: u_int = 0;
    let mut sx: u_int = 0;
    let mut sy: u_int = 0;
    if !(*c).flags & 0x1 as i32 as u64 != 0 {
        return;
    }
    (*c).tty.oflag = tty_window_offset1(&mut (*c).tty, &mut ox, &mut oy, &mut sx, &mut sy);
    if ox == (*c).tty.oox && oy == (*c).tty.ooy && sx == (*c).tty.osx && sy == (*c).tty.osy {
        return;
    }
    log_debug(
        b"%s: %s offset has changed (%u,%u %ux%u -> %u,%u %ux%u)\0" as *const u8
            as *const i8,
        (*::core::mem::transmute::<&[u8; 25], &[i8; 25]>(b"tty_update_client_offset\0"))
            .as_ptr(),
        (*c).name,
        (*c).tty.oox,
        (*c).tty.ooy,
        (*c).tty.osx,
        (*c).tty.osy,
        ox,
        oy,
        sx,
        sy,
    );
    (*c).tty.oox = ox;
    (*c).tty.ooy = oy;
    (*c).tty.osx = sx;
    (*c).tty.osy = sy;
    (*c).flags |= (0x8 as i32 | 0x10 as i32) as u64;
}
unsafe extern "C" fn tty_large_region(mut tty: *mut tty, mut ctx: *const tty_ctx) -> i32 {
    return (((*ctx).orlower).wrapping_sub((*ctx).orupper)
        >= ((*ctx).sy).wrapping_div(2 as i32 as u32)) as i32;
}
unsafe extern "C" fn tty_fake_bce(
    mut tty: *const tty,
    mut gc: *const grid_cell,
    mut bg: u_int,
) -> i32 {
    if tty_term_flag((*tty).term, TTYC_BCE) != 0 {
        return 0 as i32;
    }
    if !(bg == 8 as i32 as u32 || bg == 9 as i32 as u32)
        || !((*gc).bg == 8 as i32 || (*gc).bg == 9 as i32)
    {
        return 1 as i32;
    }
    return 0 as i32;
}
unsafe extern "C" fn tty_redraw_region(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = (*tty).client;
    let mut i: u_int = 0;
    if tty_large_region(tty, ctx) != 0 {
        log_debug(
            b"%s: %s large redraw\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 18], &[i8; 18]>(b"tty_redraw_region\0"))
                .as_ptr(),
            (*c).name,
        );
        ((*ctx).redraw_cb).expect("non-null function pointer")(ctx);
        return;
    }
    i = (*ctx).orupper;
    while i <= (*ctx).orlower {
        tty_draw_pane(tty, ctx, i);
        i = i.wrapping_add(1);
    }
}
unsafe extern "C" fn tty_is_visible(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut px: u_int,
    mut py: u_int,
    mut nx: u_int,
    mut ny: u_int,
) -> i32 {
    let mut xoff: u_int = ((*ctx).rxoff).wrapping_add(px);
    let mut yoff: u_int = ((*ctx).ryoff).wrapping_add(py);
    if (*ctx).bigger == 0 {
        return 1 as i32;
    }
    if xoff.wrapping_add(nx) <= (*ctx).wox
        || xoff >= ((*ctx).wox).wrapping_add((*ctx).wsx)
        || yoff.wrapping_add(ny) <= (*ctx).woy
        || yoff >= ((*ctx).woy).wrapping_add((*ctx).wsy)
    {
        return 0 as i32;
    }
    return 1 as i32;
}
unsafe extern "C" fn tty_clamp_line(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut px: u_int,
    mut py: u_int,
    mut nx: u_int,
    mut i: *mut u_int,
    mut x: *mut u_int,
    mut rx: *mut u_int,
    mut ry: *mut u_int,
) -> i32 {
    let mut xoff: u_int = ((*ctx).rxoff).wrapping_add(px);
    if tty_is_visible(tty, ctx, px, py, nx, 1 as i32 as u_int) == 0 {
        return 0 as i32;
    }
    *ry = ((*ctx).yoff).wrapping_add(py).wrapping_sub((*ctx).woy);
    if xoff >= (*ctx).wox && xoff.wrapping_add(nx) <= ((*ctx).wox).wrapping_add((*ctx).wsx) {
        *i = 0 as i32 as u_int;
        *x = ((*ctx).xoff).wrapping_add(px).wrapping_sub((*ctx).wox);
        *rx = nx;
    } else if xoff < (*ctx).wox && xoff.wrapping_add(nx) > ((*ctx).wox).wrapping_add((*ctx).wsx) {
        *i = (*ctx).wox;
        *x = 0 as i32 as u_int;
        *rx = (*ctx).wsx;
    } else if xoff < (*ctx).wox {
        *i = ((*ctx).wox).wrapping_sub(((*ctx).xoff).wrapping_add(px));
        *x = 0 as i32 as u_int;
        *rx = nx.wrapping_sub(*i);
    } else {
        *i = 0 as i32 as u_int;
        *x = ((*ctx).xoff).wrapping_add(px).wrapping_sub((*ctx).wox);
        *rx = ((*ctx).wsx).wrapping_sub(*x);
    }
    if *rx > nx {
        fatalx(
            b"%s: x too big, %u > %u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 15], &[i8; 15]>(b"tty_clamp_line\0"))
                .as_ptr(),
            *rx,
            nx,
        );
    }
    return 1 as i32;
}
unsafe extern "C" fn tty_clear_line(
    mut tty: *mut tty,
    mut defaults: *const grid_cell,
    mut py: u_int,
    mut px: u_int,
    mut nx: u_int,
    mut bg: u_int,
) {
    let mut c: *mut client = (*tty).client;
    let mut r: overlay_ranges = overlay_ranges {
        px: [0; 3],
        nx: [0; 3],
    };
    let mut i: u_int = 0;
    log_debug(
        b"%s: %s, %u at %u,%u\0" as *const u8 as *const i8,
        (*::core::mem::transmute::<&[u8; 15], &[i8; 15]>(b"tty_clear_line\0")).as_ptr(),
        (*c).name,
        nx,
        px,
        py,
    );
    if nx == 0 as i32 as u32 {
        return;
    }
    if ((*c).overlay_check).is_none() && tty_fake_bce(tty, defaults, bg) == 0 {
        if px.wrapping_add(nx) >= (*tty).sx && tty_term_has((*tty).term, TTYC_EL) != 0 {
            tty_cursor(tty, px, py);
            tty_putcode(tty, TTYC_EL);
            return;
        }
        if px == 0 as i32 as u32 && tty_term_has((*tty).term, TTYC_EL1) != 0 {
            tty_cursor(
                tty,
                px.wrapping_add(nx)
                    .wrapping_sub(1 as i32 as u32),
                py,
            );
            tty_putcode(tty, TTYC_EL1);
            return;
        }
        if tty_term_has((*tty).term, TTYC_ECH) != 0 {
            tty_cursor(tty, px, py);
            tty_putcode_i(tty, TTYC_ECH, nx as i32);
            return;
        }
    }
    tty_check_overlay_range(tty, px, py, nx, &mut r);
    i = 0 as i32 as u_int;
    while i < 3 as i32 as u32 {
        if r.nx[i as usize] != 0 as i32 as u32 {
            tty_cursor(tty, r.px[i as usize], py);
            tty_repeat_space(tty, r.nx[i as usize]);
        }
        i = i.wrapping_add(1);
    }
}
unsafe extern "C" fn tty_clear_pane_line(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut py: u_int,
    mut px: u_int,
    mut nx: u_int,
    mut bg: u_int,
) {
    let mut c: *mut client = (*tty).client;
    let mut i: u_int = 0;
    let mut x: u_int = 0;
    let mut rx: u_int = 0;
    let mut ry: u_int = 0;
    log_debug(
        b"%s: %s, %u at %u,%u\0" as *const u8 as *const i8,
        (*::core::mem::transmute::<&[u8; 20], &[i8; 20]>(b"tty_clear_pane_line\0"))
            .as_ptr(),
        (*c).name,
        nx,
        px,
        py,
    );
    if tty_clamp_line(tty, ctx, px, py, nx, &mut i, &mut x, &mut rx, &mut ry) != 0 {
        tty_clear_line(tty, &(*ctx).defaults, ry, x, rx, bg);
    }
}
unsafe extern "C" fn tty_clamp_area(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut px: u_int,
    mut py: u_int,
    mut nx: u_int,
    mut ny: u_int,
    mut i: *mut u_int,
    mut j: *mut u_int,
    mut x: *mut u_int,
    mut y: *mut u_int,
    mut rx: *mut u_int,
    mut ry: *mut u_int,
) -> i32 {
    let mut xoff: u_int = ((*ctx).rxoff).wrapping_add(px);
    let mut yoff: u_int = ((*ctx).ryoff).wrapping_add(py);
    if tty_is_visible(tty, ctx, px, py, nx, ny) == 0 {
        return 0 as i32;
    }
    if xoff >= (*ctx).wox && xoff.wrapping_add(nx) <= ((*ctx).wox).wrapping_add((*ctx).wsx) {
        *i = 0 as i32 as u_int;
        *x = ((*ctx).xoff).wrapping_add(px).wrapping_sub((*ctx).wox);
        *rx = nx;
    } else if xoff < (*ctx).wox && xoff.wrapping_add(nx) > ((*ctx).wox).wrapping_add((*ctx).wsx) {
        *i = (*ctx).wox;
        *x = 0 as i32 as u_int;
        *rx = (*ctx).wsx;
    } else if xoff < (*ctx).wox {
        *i = ((*ctx).wox).wrapping_sub(((*ctx).xoff).wrapping_add(px));
        *x = 0 as i32 as u_int;
        *rx = nx.wrapping_sub(*i);
    } else {
        *i = 0 as i32 as u_int;
        *x = ((*ctx).xoff).wrapping_add(px).wrapping_sub((*ctx).wox);
        *rx = ((*ctx).wsx).wrapping_sub(*x);
    }
    if *rx > nx {
        fatalx(
            b"%s: x too big, %u > %u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 15], &[i8; 15]>(b"tty_clamp_area\0"))
                .as_ptr(),
            *rx,
            nx,
        );
    }
    if yoff >= (*ctx).woy && yoff.wrapping_add(ny) <= ((*ctx).woy).wrapping_add((*ctx).wsy) {
        *j = 0 as i32 as u_int;
        *y = ((*ctx).yoff).wrapping_add(py).wrapping_sub((*ctx).woy);
        *ry = ny;
    } else if yoff < (*ctx).woy && yoff.wrapping_add(ny) > ((*ctx).woy).wrapping_add((*ctx).wsy) {
        *j = (*ctx).woy;
        *y = 0 as i32 as u_int;
        *ry = (*ctx).wsy;
    } else if yoff < (*ctx).woy {
        *j = ((*ctx).woy).wrapping_sub(((*ctx).yoff).wrapping_add(py));
        *y = 0 as i32 as u_int;
        *ry = ny.wrapping_sub(*j);
    } else {
        *j = 0 as i32 as u_int;
        *y = ((*ctx).yoff).wrapping_add(py).wrapping_sub((*ctx).woy);
        *ry = ((*ctx).wsy).wrapping_sub(*y);
    }
    if *ry > ny {
        fatalx(
            b"%s: y too big, %u > %u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 15], &[i8; 15]>(b"tty_clamp_area\0"))
                .as_ptr(),
            *ry,
            ny,
        );
    }
    return 1 as i32;
}
unsafe extern "C" fn tty_clear_area(
    mut tty: *mut tty,
    mut defaults: *const grid_cell,
    mut py: u_int,
    mut ny: u_int,
    mut px: u_int,
    mut nx: u_int,
    mut bg: u_int,
) {
    let mut c: *mut client = (*tty).client;
    let mut yy: u_int = 0;
    let mut tmp: [i8; 64] = [0; 64];
    log_debug(
        b"%s: %s, %u,%u at %u,%u\0" as *const u8 as *const i8,
        (*::core::mem::transmute::<&[u8; 15], &[i8; 15]>(b"tty_clear_area\0")).as_ptr(),
        (*c).name,
        nx,
        ny,
        px,
        py,
    );
    if nx == 0 as i32 as u32 || ny == 0 as i32 as u32 {
        return;
    }
    if ((*c).overlay_check).is_none() && tty_fake_bce(tty, defaults, bg) == 0 {
        if px == 0 as i32 as u32
            && px.wrapping_add(nx) >= (*tty).sx
            && py.wrapping_add(ny) >= (*tty).sy
            && tty_term_has((*tty).term, TTYC_ED) != 0
        {
            tty_cursor(tty, 0 as i32 as u_int, py);
            tty_putcode(tty, TTYC_ED);
            return;
        }
        if (*(*tty).term).flags & 0x8 as i32 != 0
            && !(bg == 8 as i32 as u32 || bg == 9 as i32 as u32)
        {
            xsnprintf(
                tmp.as_mut_ptr(),
                ::core::mem::size_of::<[i8; 64]>() as u64,
                b"\x1B[32;%u;%u;%u;%u$x\0" as *const u8 as *const i8,
                py.wrapping_add(1 as i32 as u32),
                px.wrapping_add(1 as i32 as u32),
                py.wrapping_add(ny),
                px.wrapping_add(nx),
            );
            tty_puts(tty, tmp.as_mut_ptr());
            return;
        }
        if px == 0 as i32 as u32
            && px.wrapping_add(nx) >= (*tty).sx
            && ny > 2 as i32 as u32
            && tty_term_has((*tty).term, TTYC_CSR) != 0
            && tty_term_has((*tty).term, TTYC_INDN) != 0
        {
            tty_region(
                tty,
                py,
                py.wrapping_add(ny)
                    .wrapping_sub(1 as i32 as u32),
            );
            tty_margin_off(tty);
            tty_putcode_i(tty, TTYC_INDN, ny as i32);
            return;
        }
        if nx > 2 as i32 as u32
            && ny > 2 as i32 as u32
            && tty_term_has((*tty).term, TTYC_CSR) != 0
            && (*(*tty).term).flags & 0x4 as i32 != 0
            && tty_term_has((*tty).term, TTYC_INDN) != 0
        {
            tty_region(
                tty,
                py,
                py.wrapping_add(ny)
                    .wrapping_sub(1 as i32 as u32),
            );
            tty_margin(
                tty,
                px,
                px.wrapping_add(nx)
                    .wrapping_sub(1 as i32 as u32),
            );
            tty_putcode_i(tty, TTYC_INDN, ny as i32);
            return;
        }
    }
    yy = py;
    while yy < py.wrapping_add(ny) {
        tty_clear_line(tty, defaults, yy, px, nx, bg);
        yy = yy.wrapping_add(1);
    }
}
unsafe extern "C" fn tty_clear_pane_area(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut py: u_int,
    mut ny: u_int,
    mut px: u_int,
    mut nx: u_int,
    mut bg: u_int,
) {
    let mut i: u_int = 0;
    let mut j: u_int = 0;
    let mut x: u_int = 0;
    let mut y: u_int = 0;
    let mut rx: u_int = 0;
    let mut ry: u_int = 0;
    if tty_clamp_area(
        tty, ctx, px, py, nx, ny, &mut i, &mut j, &mut x, &mut y, &mut rx, &mut ry,
    ) != 0
    {
        tty_clear_area(tty, &(*ctx).defaults, y, ry, x, rx, bg);
    }
}
unsafe extern "C" fn tty_draw_pane(mut tty: *mut tty, mut ctx: *const tty_ctx, mut py: u_int) {
    let mut s: *mut screen = (*ctx).s;
    let mut nx: u_int = (*ctx).sx;
    let mut i: u_int = 0;
    let mut x: u_int = 0;
    let mut rx: u_int = 0;
    let mut ry: u_int = 0;
    log_debug(
        b"%s: %s %u %d\0" as *const u8 as *const i8,
        (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_pane\0")).as_ptr(),
        (*(*tty).client).name,
        py,
        (*ctx).bigger,
    );
    if (*ctx).bigger == 0 {
        tty_draw_line(
            tty,
            s,
            0 as i32 as u_int,
            py,
            nx,
            (*ctx).xoff,
            ((*ctx).yoff).wrapping_add(py),
            &(*ctx).defaults,
            (*ctx).palette,
        );
        return;
    }
    if tty_clamp_line(
        tty,
        ctx,
        0 as i32 as u_int,
        py,
        nx,
        &mut i,
        &mut x,
        &mut rx,
        &mut ry,
    ) != 0
    {
        tty_draw_line(tty, s, i, py, rx, x, ry, &(*ctx).defaults, (*ctx).palette);
    }
}
unsafe extern "C" fn tty_check_codeset(
    mut tty: *mut tty,
    mut gc: *const grid_cell,
) -> *const grid_cell {
    static mut new: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut c: i32 = 0;
    if (*gc).data.size as i32 == 1 as i32
        && (*((*gc).data.data).as_ptr() as i32) < 0x7f as i32
    {
        return gc;
    }
    if (*gc).flags as i32 & 0x80 as i32 != 0 {
        return gc;
    }
    if (*(*tty).client).flags & 0x10000 as i32 as u64 != 0 {
        return gc;
    }
    memcpy(
        &mut new as *mut grid_cell as *mut libc::c_void,
        gc as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
    c = tty_acs_reverse_get(
        tty,
        ((*gc).data.data).as_ptr() as *const i8,
        (*gc).data.size as size_t,
    );
    if c != -(1 as i32) {
        utf8_set(&mut new.data, c as u_char);
        new.attr = (new.attr as i32 | 0x80 as i32) as u_short;
        return &mut new;
    }
    new.data.size = (*gc).data.width;
    if new.data.size as i32 > 21 as i32 {
        new.data.size = 21 as i32 as u_char;
    }
    memset(
        (new.data.data).as_mut_ptr() as *mut libc::c_void,
        '_' as i32,
        new.data.size as u64,
    );
    return &mut new;
}
unsafe extern "C" fn tty_check_overlay(
    mut tty: *mut tty,
    mut px: u_int,
    mut py: u_int,
) -> i32 {
    let mut r: overlay_ranges = overlay_ranges {
        px: [0; 3],
        nx: [0; 3],
    };
    tty_check_overlay_range(tty, px, py, 1 as i32 as u_int, &mut r);
    if (r.nx[0 as i32 as usize]).wrapping_add(r.nx[1 as i32 as usize])
        == 0 as i32 as u32
    {
        return 0 as i32;
    }
    return 1 as i32;
}
unsafe extern "C" fn tty_check_overlay_range(
    mut tty: *mut tty,
    mut px: u_int,
    mut py: u_int,
    mut nx: u_int,
    mut r: *mut overlay_ranges,
) {
    let mut c: *mut client = (*tty).client;
    if ((*c).overlay_check).is_none() {
        (*r).px[0 as i32 as usize] = px;
        (*r).nx[0 as i32 as usize] = nx;
        (*r).px[1 as i32 as usize] = 0 as i32 as u_int;
        (*r).nx[1 as i32 as usize] = 0 as i32 as u_int;
        (*r).px[2 as i32 as usize] = 0 as i32 as u_int;
        (*r).nx[2 as i32 as usize] = 0 as i32 as u_int;
        return;
    }
    ((*c).overlay_check).expect("non-null function pointer")(c, (*c).overlay_data, px, py, nx, r);
}
#[no_mangle]
pub unsafe extern "C" fn tty_draw_line(
    mut tty: *mut tty,
    mut s: *mut screen,
    mut px: u_int,
    mut py: u_int,
    mut nx: u_int,
    mut atx: u_int,
    mut aty: u_int,
    mut defaults: *const grid_cell,
    mut palette: *mut colour_palette,
) {
    let mut gd: *mut grid = (*s).grid;
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut last: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut gcp: *const grid_cell = std::ptr::null::<grid_cell>();
    let mut gl: *mut grid_line = std::ptr::null_mut::<grid_line>();
    let mut c: *mut client = (*tty).client;
    let mut r: overlay_ranges = overlay_ranges {
        px: [0; 3],
        nx: [0; 3],
    };
    let mut i: u_int = 0;
    let mut j: u_int = 0;
    let mut ux: u_int = 0;
    let mut sx: u_int = 0;
    let mut width: u_int = 0;
    let mut hidden: u_int = 0;
    let mut eux: u_int = 0;
    let mut nxx: u_int = 0;
    let mut cellsize: u_int = 0;
    let mut flags: i32 = 0;
    let mut cleared: i32 = 0 as i32;
    let mut wrapped: i32 = 0 as i32;
    let mut buf: [i8; 512] = [0; 512];
    let mut len: size_t = 0;
    log_debug(
        b"%s: px=%u py=%u nx=%u atx=%u aty=%u\0" as *const u8 as *const i8,
        (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_line\0")).as_ptr(),
        px,
        py,
        nx,
        atx,
        aty,
    );
    log_debug(
        b"%s: defaults: fg=%d, bg=%d\0" as *const u8 as *const i8,
        (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_line\0")).as_ptr(),
        (*defaults).fg,
        (*defaults).bg,
    );
    flags = (*tty).flags & 0x1 as i32;
    (*tty).flags |= 0x1 as i32;
    tty_update_mode(tty, (*tty).mode, s);
    tty_region_off(tty);
    tty_margin_off(tty);
    sx = (*(*s).grid).sx;
    if nx > sx {
        nx = sx;
    }
    cellsize = (*grid_get_line(gd, ((*gd).hsize).wrapping_add(py))).cellsize;
    if sx > cellsize {
        sx = cellsize;
    }
    if sx > (*tty).sx {
        sx = (*tty).sx;
    }
    if sx > nx {
        sx = nx;
    }
    ux = 0 as i32 as u_int;
    if py == 0 as i32 as u32 {
        gl = std::ptr::null_mut::<grid_line>();
    } else {
        gl = grid_get_line(
            gd,
            ((*gd).hsize)
                .wrapping_add(py)
                .wrapping_sub(1 as i32 as u32),
        );
    }
    if gl.is_null()
        || !(*gl).flags & 0x1 as i32 != 0
        || atx != 0 as i32 as u32
        || (*tty).cx < (*tty).sx
        || nx < (*tty).sx
    {
        if nx < (*tty).sx
            && atx == 0 as i32 as u32
            && px.wrapping_add(sx) != nx
            && tty_term_has((*tty).term, TTYC_EL1) != 0
            && tty_fake_bce(tty, defaults, 8 as i32 as u_int) == 0
            && ((*c).overlay_check).is_none()
        {
            tty_default_attributes(
                tty,
                defaults,
                palette,
                8 as i32 as u_int,
                (*s).hyperlinks,
            );
            tty_cursor(tty, nx.wrapping_sub(1 as i32 as u32), aty);
            tty_putcode(tty, TTYC_EL1);
            cleared = 1 as i32;
        }
    } else {
        log_debug(
            b"%s: wrapped line %u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_line\0"))
                .as_ptr(),
            aty,
        );
        wrapped = 1 as i32;
    }
    memcpy(
        &mut last as *mut grid_cell as *mut libc::c_void,
        &grid_default_cell as *const grid_cell as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
    len = 0 as i32 as size_t;
    width = 0 as i32 as u_int;
    i = 0 as i32 as u_int;
    while i < sx {
        grid_view_get_cell(gd, px.wrapping_add(i), py, &mut gc);
        gcp = tty_check_codeset(tty, &mut gc);
        if len != 0 as i32 as u64
            && (tty_check_overlay(tty, atx.wrapping_add(ux).wrapping_add(width), aty) == 0
                || (*gcp).attr as i32 & 0x80 as i32 != 0
                || (*gcp).flags as i32 != last.flags as i32
                || (*gcp).attr as i32 != last.attr as i32
                || (*gcp).fg != last.fg
                || (*gcp).bg != last.bg
                || (*gcp).us != last.us
                || (*gcp).link != last.link
                || ux
                    .wrapping_add(width)
                    .wrapping_add((*gcp).data.width as u32)
                    > nx
                || (::core::mem::size_of::<[i8; 512]>() as u64)
                    .wrapping_sub(len)
                    < (*gcp).data.size as u64)
        {
            tty_attributes(tty, &mut last, defaults, palette, (*s).hyperlinks);
            if last.flags as i32 & 0x40 as i32 != 0 {
                log_debug(
                    b"%s: %zu cleared\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_line\0"))
                        .as_ptr(),
                    len,
                );
                tty_clear_line(
                    tty,
                    defaults,
                    aty,
                    atx.wrapping_add(ux),
                    width,
                    last.bg as u_int,
                );
            } else {
                if wrapped == 0
                    || atx != 0 as i32 as u32
                    || ux != 0 as i32 as u32
                {
                    tty_cursor(tty, atx.wrapping_add(ux), aty);
                }
                tty_putn(tty, buf.as_mut_ptr() as *const libc::c_void, len, width);
            }
            ux = (ux as u32).wrapping_add(width) as u_int as u_int;
            len = 0 as i32 as size_t;
            width = 0 as i32 as u_int;
            wrapped = 0 as i32;
        }
        if (*gcp).flags as i32 & 0x10 as i32 != 0 {
            screen_select_cell(s, &mut last, gcp);
        } else {
            memcpy(
                &mut last as *mut grid_cell as *mut libc::c_void,
                gcp as *const libc::c_void,
                ::core::mem::size_of::<grid_cell>() as u64,
            );
        }
        tty_check_overlay_range(
            tty,
            atx.wrapping_add(ux),
            aty,
            (*gcp).data.width as u_int,
            &mut r,
        );
        hidden = 0 as i32 as u_int;
        j = 0 as i32 as u_int;
        while j < 3 as i32 as u32 {
            hidden = (hidden as u32).wrapping_add(r.nx[j as usize]) as u_int as u_int;
            j = j.wrapping_add(1);
        }
        hidden = ((*gcp).data.width as u32).wrapping_sub(hidden);
        if hidden != 0 as i32 as u32 && hidden == (*gcp).data.width as u32
        {
            if !((*gcp).flags as i32) & 0x4 as i32 != 0 {
                ux = (ux as u32).wrapping_add((*gcp).data.width as u32) as u_int
                    as u_int;
            }
        } else if hidden != 0 as i32 as u32
            || ux.wrapping_add((*gcp).data.width as u32) > nx
        {
            if !((*gcp).flags as i32) & 0x4 as i32 != 0 {
                tty_attributes(tty, &mut last, defaults, palette, (*s).hyperlinks);
                j = 0 as i32 as u_int;
                while j < 3 as i32 as u32 {
                    if r.nx[j as usize] != 0 as i32 as u32 {
                        eux = (r.px[j as usize]).wrapping_sub(atx);
                        if eux < nx {
                            tty_cursor(tty, r.px[j as usize], aty);
                            nxx = nx.wrapping_sub(eux);
                            if r.nx[j as usize] > nxx {
                                r.nx[j as usize] = nxx;
                            }
                            tty_repeat_space(tty, r.nx[j as usize]);
                            ux = eux.wrapping_add(r.nx[j as usize]);
                        }
                    }
                    j = j.wrapping_add(1);
                }
            }
        } else if (*gcp).attr as i32 & 0x80 as i32 != 0 {
            tty_attributes(tty, &mut last, defaults, palette, (*s).hyperlinks);
            tty_cursor(tty, atx.wrapping_add(ux), aty);
            j = 0 as i32 as u_int;
            while j < (*gcp).data.size as u32 {
                tty_putc(tty, (*gcp).data.data[j as usize]);
                j = j.wrapping_add(1);
            }
            ux = (ux as u32).wrapping_add((*gcp).data.width as u32) as u_int
                as u_int;
        } else if !((*gcp).flags as i32) & 0x4 as i32 != 0 {
            memcpy(
                buf.as_mut_ptr().offset(len as isize) as *mut libc::c_void,
                ((*gcp).data.data).as_ptr() as *const libc::c_void,
                (*gcp).data.size as u64,
            );
            len = (len as u64).wrapping_add((*gcp).data.size as u64) as size_t
                as size_t;
            width = (width as u32).wrapping_add((*gcp).data.width as u32) as u_int
                as u_int;
        }
        i = i.wrapping_add(1);
    }
    if len != 0 as i32 as u64
        && (!(last.flags as i32) & 0x40 as i32 != 0 || last.bg != 8 as i32)
    {
        tty_attributes(tty, &mut last, defaults, palette, (*s).hyperlinks);
        if last.flags as i32 & 0x40 as i32 != 0 {
            log_debug(
                b"%s: %zu cleared (end)\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_line\0"))
                    .as_ptr(),
                len,
            );
            tty_clear_line(
                tty,
                defaults,
                aty,
                atx.wrapping_add(ux),
                width,
                last.bg as u_int,
            );
        } else {
            if wrapped == 0
                || atx != 0 as i32 as u32
                || ux != 0 as i32 as u32
            {
                tty_cursor(tty, atx.wrapping_add(ux), aty);
            }
            tty_putn(tty, buf.as_mut_ptr() as *const libc::c_void, len, width);
        }
        ux = (ux as u32).wrapping_add(width) as u_int as u_int;
    }
    if cleared == 0 && ux < nx {
        log_debug(
            b"%s: %u to end of line (%zu cleared)\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"tty_draw_line\0"))
                .as_ptr(),
            nx.wrapping_sub(ux),
            len,
        );
        tty_default_attributes(
            tty,
            defaults,
            palette,
            8 as i32 as u_int,
            (*s).hyperlinks,
        );
        tty_clear_line(
            tty,
            defaults,
            aty,
            atx.wrapping_add(ux),
            nx.wrapping_sub(ux),
            8 as i32 as u_int,
        );
    }
    (*tty).flags = (*tty).flags & !(0x1 as i32) | flags;
    tty_update_mode(tty, (*tty).mode, s);
}
#[no_mangle]
pub unsafe extern "C" fn tty_sync_start(mut tty: *mut tty) {
    if (*tty).flags & 0x80 as i32 != 0 {
        return;
    }
    if (*tty).flags & 0x400 as i32 != 0 {
        return;
    }
    (*tty).flags |= 0x400 as i32;
    if tty_term_has((*tty).term, TTYC_SYNC) != 0 {
        log_debug(
            b"%s sync start\0" as *const u8 as *const i8,
            (*(*tty).client).name,
        );
        tty_putcode_i(tty, TTYC_SYNC, 1 as i32);
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_sync_end(mut tty: *mut tty) {
    if (*tty).flags & 0x80 as i32 != 0 {
        return;
    }
    if !(*tty).flags & 0x400 as i32 != 0 {
        return;
    }
    (*tty).flags &= !(0x400 as i32);
    if tty_term_has((*tty).term, TTYC_SYNC) != 0 {
        log_debug(
            b"%s sync end\0" as *const u8 as *const i8,
            (*(*tty).client).name,
        );
        tty_putcode_i(tty, TTYC_SYNC, 2 as i32);
    }
}
unsafe extern "C" fn tty_client_ready(mut ctx: *const tty_ctx, mut c: *mut client) -> i32 {
    if ((*c).session).is_null() || ((*c).tty.term).is_null() {
        return 0 as i32;
    }
    if (*c).flags & 0x40 as i32 as u64 != 0 {
        return 0 as i32;
    }
    if (*ctx).allow_invisible_panes != 0 {
        return 1 as i32;
    }
    if (*c).flags & 0x8 as i32 as u64 != 0 {
        return 0 as i32;
    }
    if (*c).tty.flags & 0x2 as i32 != 0 {
        return 0 as i32;
    }
    return 1 as i32;
}
#[no_mangle]
pub unsafe extern "C" fn tty_write(
    mut cmdfn: Option<unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()>,
    mut ctx: *mut tty_ctx,
) {
    let mut c: *mut client = std::ptr::null_mut::<client>();
    let mut state: i32 = 0;
    if ((*ctx).set_client_cb).is_none() {
        return;
    }
    c = clients.tqh_first;
    while !c.is_null() {
        if tty_client_ready(ctx, c) != 0 {
            state = ((*ctx).set_client_cb).expect("non-null function pointer")(ctx, c);
            if state == -(1 as i32) {
                break;
            }
            if state != 0 as i32 {
                cmdfn.expect("non-null function pointer")(&mut (*c).tty, ctx);
            }
        }
        c = (*c).entry.tqe_next;
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_insertcharacter(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = (*tty).client;
    if (*ctx).bigger != 0
        || !((*ctx).xoff == 0 as i32 as u32 && (*ctx).sx >= (*tty).sx)
        || tty_fake_bce(tty, &(*ctx).defaults, (*ctx).bg) != 0
        || tty_term_has((*tty).term, TTYC_ICH) == 0 && tty_term_has((*tty).term, TTYC_ICH1) == 0
        || ((*c).overlay_check).is_some()
    {
        tty_draw_pane(tty, ctx, (*ctx).ocy);
        return;
    }
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_cursor_pane(tty, ctx, (*ctx).ocx, (*ctx).ocy);
    tty_emulate_repeat(tty, TTYC_ICH, TTYC_ICH1, (*ctx).num);
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_deletecharacter(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = (*tty).client;
    if (*ctx).bigger != 0
        || !((*ctx).xoff == 0 as i32 as u32 && (*ctx).sx >= (*tty).sx)
        || tty_fake_bce(tty, &(*ctx).defaults, (*ctx).bg) != 0
        || tty_term_has((*tty).term, TTYC_DCH) == 0 && tty_term_has((*tty).term, TTYC_DCH1) == 0
        || ((*c).overlay_check).is_some()
    {
        tty_draw_pane(tty, ctx, (*ctx).ocy);
        return;
    }
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_cursor_pane(tty, ctx, (*ctx).ocx, (*ctx).ocy);
    tty_emulate_repeat(tty, TTYC_DCH, TTYC_DCH1, (*ctx).num);
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_clearcharacter(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_clear_pane_line(tty, ctx, (*ctx).ocy, (*ctx).ocx, (*ctx).num, (*ctx).bg);
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_insertline(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = (*tty).client;
    if (*ctx).bigger != 0
        || !((*ctx).xoff == 0 as i32 as u32 && (*ctx).sx >= (*tty).sx)
        || tty_fake_bce(tty, &(*ctx).defaults, (*ctx).bg) != 0
        || tty_term_has((*tty).term, TTYC_CSR) == 0
        || tty_term_has((*tty).term, TTYC_IL1) == 0
        || (*ctx).sx == 1 as i32 as u32
        || (*ctx).sy == 1 as i32 as u32
        || ((*c).overlay_check).is_some()
    {
        tty_redraw_region(tty, ctx);
        return;
    }
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_region_pane(tty, ctx, (*ctx).orupper, (*ctx).orlower);
    tty_margin_off(tty);
    tty_cursor_pane(tty, ctx, (*ctx).ocx, (*ctx).ocy);
    tty_emulate_repeat(tty, TTYC_IL, TTYC_IL1, (*ctx).num);
    (*tty).cy = (2147483647 as i32 as u32)
        .wrapping_mul(2 as u32)
        .wrapping_add(1 as u32);
    (*tty).cx = (*tty).cy;
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_deleteline(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = (*tty).client;
    if (*ctx).bigger != 0
        || !((*ctx).xoff == 0 as i32 as u32 && (*ctx).sx >= (*tty).sx)
        || tty_fake_bce(tty, &(*ctx).defaults, (*ctx).bg) != 0
        || tty_term_has((*tty).term, TTYC_CSR) == 0
        || tty_term_has((*tty).term, TTYC_DL1) == 0
        || (*ctx).sx == 1 as i32 as u32
        || (*ctx).sy == 1 as i32 as u32
        || ((*c).overlay_check).is_some()
    {
        tty_redraw_region(tty, ctx);
        return;
    }
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_region_pane(tty, ctx, (*ctx).orupper, (*ctx).orlower);
    tty_margin_off(tty);
    tty_cursor_pane(tty, ctx, (*ctx).ocx, (*ctx).ocy);
    tty_emulate_repeat(tty, TTYC_DL, TTYC_DL1, (*ctx).num);
    (*tty).cy = (2147483647 as i32 as u32)
        .wrapping_mul(2 as u32)
        .wrapping_add(1 as u32);
    (*tty).cx = (*tty).cy;
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_clearline(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_clear_pane_line(
        tty,
        ctx,
        (*ctx).ocy,
        0 as i32 as u_int,
        (*ctx).sx,
        (*ctx).bg,
    );
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_clearendofline(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut nx: u_int = ((*ctx).sx).wrapping_sub((*ctx).ocx);
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_clear_pane_line(tty, ctx, (*ctx).ocy, (*ctx).ocx, nx, (*ctx).bg);
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_clearstartofline(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_clear_pane_line(
        tty,
        ctx,
        (*ctx).ocy,
        0 as i32 as u_int,
        ((*ctx).ocx).wrapping_add(1 as i32 as u32),
        (*ctx).bg,
    );
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_reverseindex(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = (*tty).client;
    if (*ctx).ocy != (*ctx).orupper {
        return;
    }
    if (*ctx).bigger != 0
        || !((*ctx).xoff == 0 as i32 as u32 && (*ctx).sx >= (*tty).sx)
            && (*(*tty).term).flags & 0x4 as i32 == 0
        || tty_fake_bce(tty, &(*ctx).defaults, 8 as i32 as u_int) != 0
        || tty_term_has((*tty).term, TTYC_CSR) == 0
        || tty_term_has((*tty).term, TTYC_RI) == 0 && tty_term_has((*tty).term, TTYC_RIN) == 0
        || (*ctx).sx == 1 as i32 as u32
        || (*ctx).sy == 1 as i32 as u32
        || ((*c).overlay_check).is_some()
    {
        tty_redraw_region(tty, ctx);
        return;
    }
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_region_pane(tty, ctx, (*ctx).orupper, (*ctx).orlower);
    tty_margin_pane(tty, ctx);
    tty_cursor_pane(tty, ctx, (*ctx).ocx, (*ctx).orupper);
    if tty_term_has((*tty).term, TTYC_RI) != 0 {
        tty_putcode(tty, TTYC_RI);
    } else {
        tty_putcode_i(tty, TTYC_RIN, 1 as i32);
    };
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_linefeed(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = (*tty).client;
    if (*ctx).ocy != (*ctx).orlower {
        return;
    }
    if (*ctx).bigger != 0
        || !((*ctx).xoff == 0 as i32 as u32 && (*ctx).sx >= (*tty).sx)
            && (*(*tty).term).flags & 0x4 as i32 == 0
        || tty_fake_bce(tty, &(*ctx).defaults, 8 as i32 as u_int) != 0
        || tty_term_has((*tty).term, TTYC_CSR) == 0
        || (*ctx).sx == 1 as i32 as u32
        || (*ctx).sy == 1 as i32 as u32
        || ((*c).overlay_check).is_some()
    {
        tty_redraw_region(tty, ctx);
        return;
    }
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_region_pane(tty, ctx, (*ctx).orupper, (*ctx).orlower);
    tty_margin_pane(tty, ctx);
    if ((*ctx).xoff).wrapping_add((*ctx).ocx) > (*tty).rright {
        if (*(*tty).term).flags & 0x4 as i32 == 0 {
            tty_cursor(
                tty,
                0 as i32 as u_int,
                ((*ctx).yoff).wrapping_add((*ctx).ocy),
            );
        } else {
            tty_cursor(tty, (*tty).rright, ((*ctx).yoff).wrapping_add((*ctx).ocy));
        }
    } else {
        tty_cursor_pane(tty, ctx, (*ctx).ocx, (*ctx).ocy);
    }
    tty_putc(tty, '\n' as i32 as u_char);
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_scrollup(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut c: *mut client = (*tty).client;
    let mut i: u_int = 0;
    if (*ctx).bigger != 0
        || !((*ctx).xoff == 0 as i32 as u32 && (*ctx).sx >= (*tty).sx)
            && (*(*tty).term).flags & 0x4 as i32 == 0
        || tty_fake_bce(tty, &(*ctx).defaults, 8 as i32 as u_int) != 0
        || tty_term_has((*tty).term, TTYC_CSR) == 0
        || (*ctx).sx == 1 as i32 as u32
        || (*ctx).sy == 1 as i32 as u32
        || ((*c).overlay_check).is_some()
    {
        tty_redraw_region(tty, ctx);
        return;
    }
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_region_pane(tty, ctx, (*ctx).orupper, (*ctx).orlower);
    tty_margin_pane(tty, ctx);
    if (*ctx).num == 1 as i32 as u32 || tty_term_has((*tty).term, TTYC_INDN) == 0 {
        if (*(*tty).term).flags & 0x4 as i32 == 0 {
            tty_cursor(tty, 0 as i32 as u_int, (*tty).rlower);
        } else {
            tty_cursor(tty, (*tty).rright, (*tty).rlower);
        }
        i = 0 as i32 as u_int;
        while i < (*ctx).num {
            tty_putc(tty, '\n' as i32 as u_char);
            i = i.wrapping_add(1);
        }
    } else {
        if (*tty).cy
            == (2147483647 as i32 as u32)
                .wrapping_mul(2 as u32)
                .wrapping_add(1 as u32)
        {
            tty_cursor(tty, 0 as i32 as u_int, 0 as i32 as u_int);
        } else {
            tty_cursor(tty, 0 as i32 as u_int, (*tty).cy);
        }
        tty_putcode_i(tty, TTYC_INDN, (*ctx).num as i32);
    };
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_scrolldown(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut i: u_int = 0;
    let mut c: *mut client = (*tty).client;
    if (*ctx).bigger != 0
        || !((*ctx).xoff == 0 as i32 as u32 && (*ctx).sx >= (*tty).sx)
            && (*(*tty).term).flags & 0x4 as i32 == 0
        || tty_fake_bce(tty, &(*ctx).defaults, 8 as i32 as u_int) != 0
        || tty_term_has((*tty).term, TTYC_CSR) == 0
        || tty_term_has((*tty).term, TTYC_RI) == 0 && tty_term_has((*tty).term, TTYC_RIN) == 0
        || (*ctx).sx == 1 as i32 as u32
        || (*ctx).sy == 1 as i32 as u32
        || ((*c).overlay_check).is_some()
    {
        tty_redraw_region(tty, ctx);
        return;
    }
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_region_pane(tty, ctx, (*ctx).orupper, (*ctx).orlower);
    tty_margin_pane(tty, ctx);
    tty_cursor_pane(tty, ctx, (*ctx).ocx, (*ctx).orupper);
    if tty_term_has((*tty).term, TTYC_RIN) != 0 {
        tty_putcode_i(tty, TTYC_RIN, (*ctx).num as i32);
    } else {
        i = 0 as i32 as u_int;
        while i < (*ctx).num {
            tty_putcode(tty, TTYC_RI);
            i = i.wrapping_add(1);
        }
    };
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_clearendofscreen(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut nx: u_int = 0;
    let mut ny: u_int = 0;
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_region_pane(
        tty,
        ctx,
        0 as i32 as u_int,
        ((*ctx).sy).wrapping_sub(1 as i32 as u32),
    );
    tty_margin_off(tty);
    px = 0 as i32 as u_int;
    nx = (*ctx).sx;
    py = ((*ctx).ocy).wrapping_add(1 as i32 as u32);
    ny = ((*ctx).sy)
        .wrapping_sub((*ctx).ocy)
        .wrapping_sub(1 as i32 as u32);
    tty_clear_pane_area(tty, ctx, py, ny, px, nx, (*ctx).bg);
    px = (*ctx).ocx;
    nx = ((*ctx).sx).wrapping_sub((*ctx).ocx);
    py = (*ctx).ocy;
    tty_clear_pane_line(tty, ctx, py, px, nx, (*ctx).bg);
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_clearstartofscreen(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut nx: u_int = 0;
    let mut ny: u_int = 0;
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_region_pane(
        tty,
        ctx,
        0 as i32 as u_int,
        ((*ctx).sy).wrapping_sub(1 as i32 as u32),
    );
    tty_margin_off(tty);
    px = 0 as i32 as u_int;
    nx = (*ctx).sx;
    py = 0 as i32 as u_int;
    ny = (*ctx).ocy;
    tty_clear_pane_area(tty, ctx, py, ny, px, nx, (*ctx).bg);
    px = 0 as i32 as u_int;
    nx = ((*ctx).ocx).wrapping_add(1 as i32 as u32);
    py = (*ctx).ocy;
    tty_clear_pane_line(tty, ctx, py, px, nx, (*ctx).bg);
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_clearscreen(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut nx: u_int = 0;
    let mut ny: u_int = 0;
    tty_default_attributes(
        tty,
        &(*ctx).defaults,
        (*ctx).palette,
        (*ctx).bg,
        (*(*ctx).s).hyperlinks,
    );
    tty_region_pane(
        tty,
        ctx,
        0 as i32 as u_int,
        ((*ctx).sy).wrapping_sub(1 as i32 as u32),
    );
    tty_margin_off(tty);
    px = 0 as i32 as u_int;
    nx = (*ctx).sx;
    py = 0 as i32 as u_int;
    ny = (*ctx).sy;
    tty_clear_pane_area(tty, ctx, py, ny, px, nx, (*ctx).bg);
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_alignmenttest(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut i: u_int = 0;
    let mut j: u_int = 0;
    if (*ctx).bigger != 0 {
        ((*ctx).redraw_cb).expect("non-null function pointer")(ctx);
        return;
    }
    tty_attributes(
        tty,
        &grid_default_cell,
        &(*ctx).defaults,
        (*ctx).palette,
        (*(*ctx).s).hyperlinks,
    );
    tty_region_pane(
        tty,
        ctx,
        0 as i32 as u_int,
        ((*ctx).sy).wrapping_sub(1 as i32 as u32),
    );
    tty_margin_off(tty);
    j = 0 as i32 as u_int;
    while j < (*ctx).sy {
        tty_cursor_pane(tty, ctx, 0 as i32 as u_int, j);
        i = 0 as i32 as u_int;
        while i < (*ctx).sx {
            tty_putc(tty, 'E' as i32 as u_char);
            i = i.wrapping_add(1);
        }
        j = j.wrapping_add(1);
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_cell(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut gcp: *const grid_cell = (*ctx).cell;
    let mut s: *mut screen = (*ctx).s;
    let mut r: overlay_ranges = overlay_ranges {
        px: [0; 3],
        nx: [0; 3],
    };
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut i: u_int = 0;
    let mut vis: u_int = 0 as i32 as u_int;
    px = ((*ctx).xoff)
        .wrapping_add((*ctx).ocx)
        .wrapping_sub((*ctx).wox);
    py = ((*ctx).yoff)
        .wrapping_add((*ctx).ocy)
        .wrapping_sub((*ctx).woy);
    if tty_is_visible(
        tty,
        ctx,
        (*ctx).ocx,
        (*ctx).ocy,
        1 as i32 as u_int,
        1 as i32 as u_int,
    ) == 0
        || (*gcp).data.width as i32 == 1 as i32
            && tty_check_overlay(tty, px, py) == 0
    {
        return;
    }
    if (*gcp).data.width as i32 > 1 as i32 {
        tty_check_overlay_range(tty, px, py, (*gcp).data.width as u_int, &mut r);
        i = 0 as i32 as u_int;
        while i < 3 as i32 as u32 {
            vis = (vis as u32).wrapping_add(r.nx[i as usize]) as u_int as u_int;
            i = i.wrapping_add(1);
        }
        if vis < (*gcp).data.width as u32 {
            tty_draw_line(
                tty,
                s,
                (*s).cx,
                (*s).cy,
                (*gcp).data.width as u_int,
                px,
                py,
                &(*ctx).defaults,
                (*ctx).palette,
            );
            return;
        }
    }
    if ((*ctx).xoff)
        .wrapping_add((*ctx).ocx)
        .wrapping_sub((*ctx).wox)
        > ((*tty).sx).wrapping_sub(1 as i32 as u32)
        && (*ctx).ocy == (*ctx).orlower
        && ((*ctx).xoff == 0 as i32 as u32 && (*ctx).sx >= (*tty).sx)
    {
        tty_region_pane(tty, ctx, (*ctx).orupper, (*ctx).orlower);
    }
    tty_margin_off(tty);
    tty_cursor_pane_unless_wrap(tty, ctx, (*ctx).ocx, (*ctx).ocy);
    tty_cell(
        tty,
        (*ctx).cell,
        &(*ctx).defaults,
        (*ctx).palette,
        (*(*ctx).s).hyperlinks,
    );
    if (*ctx).num == 1 as i32 as u32 {
        tty_invalidate(tty);
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_cells(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    let mut r: overlay_ranges = overlay_ranges {
        px: [0; 3],
        nx: [0; 3],
    };
    let mut i: u_int = 0;
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    let mut cx: u_int = 0;
    let mut cp: *mut i8 = (*ctx).ptr as *mut i8;
    if tty_is_visible(
        tty,
        ctx,
        (*ctx).ocx,
        (*ctx).ocy,
        (*ctx).num,
        1 as i32 as u_int,
    ) == 0
    {
        return;
    }
    if (*ctx).bigger != 0
        && (((*ctx).xoff).wrapping_add((*ctx).ocx) < (*ctx).wox
            || ((*ctx).xoff)
                .wrapping_add((*ctx).ocx)
                .wrapping_add((*ctx).num)
                > ((*ctx).wox).wrapping_add((*ctx).wsx))
    {
        if (*ctx).wrapped == 0
            || !((*ctx).xoff == 0 as i32 as u32 && (*ctx).sx >= (*tty).sx)
            || (*(*tty).term).flags & 0x2 as i32 != 0
            || ((*ctx).xoff).wrapping_add((*ctx).ocx) != 0 as i32 as u32
            || ((*ctx).yoff).wrapping_add((*ctx).ocy)
                != ((*tty).cy).wrapping_add(1 as i32 as u32)
            || (*tty).cx < (*tty).sx
            || (*tty).cy == (*tty).rlower
        {
            tty_draw_pane(tty, ctx, (*ctx).ocy);
        } else {
            ((*ctx).redraw_cb).expect("non-null function pointer")(ctx);
        }
        return;
    }
    tty_margin_off(tty);
    tty_cursor_pane_unless_wrap(tty, ctx, (*ctx).ocx, (*ctx).ocy);
    tty_attributes(
        tty,
        (*ctx).cell,
        &(*ctx).defaults,
        (*ctx).palette,
        (*(*ctx).s).hyperlinks,
    );
    px = ((*ctx).xoff)
        .wrapping_add((*ctx).ocx)
        .wrapping_sub((*ctx).wox);
    py = ((*ctx).yoff)
        .wrapping_add((*ctx).ocy)
        .wrapping_sub((*ctx).woy);
    tty_check_overlay_range(tty, px, py, (*ctx).num, &mut r);
    i = 0 as i32 as u_int;
    while i < 3 as i32 as u32 {
        if r.nx[i as usize] != 0 as i32 as u32 {
            cx = (r.px[i as usize])
                .wrapping_sub((*ctx).xoff)
                .wrapping_add((*ctx).wox);
            tty_cursor_pane_unless_wrap(tty, ctx, cx, (*ctx).ocy);
            tty_putn(
                tty,
                cp.offset(r.px[i as usize] as isize).offset(-(px as isize)) as *const libc::c_void,
                r.nx[i as usize] as size_t,
                r.nx[i as usize],
            );
        }
        i = i.wrapping_add(1);
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_setselection(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    tty_set_selection(
        tty,
        (*ctx).ptr2 as *const i8,
        (*ctx).ptr as *const i8,
        (*ctx).num as size_t,
    );
}
#[no_mangle]
pub unsafe extern "C" fn tty_set_selection(
    mut tty: *mut tty,
    mut flags: *const i8,
    mut buf: *const i8,
    mut len: size_t,
) {
    let mut encoded: *mut i8 = std::ptr::null_mut::<i8>();
    let mut size: size_t = 0;
    if !(*tty).flags & 0x10 as i32 != 0 {
        return;
    }
    if tty_term_has((*tty).term, TTYC_MS) == 0 {
        return;
    }
    size = (4 as i32 as u64)
        .wrapping_mul(
            len.wrapping_add(2 as i32 as u64)
                .wrapping_div(3 as i32 as u64),
        )
        .wrapping_add(1 as i32 as u64);
    encoded = xmalloc(size) as *mut i8;
    __b64_ntop(buf as *const u8, len, encoded, size);
    (*tty).flags |= 0x8 as i32;
    tty_putcode_ss(tty, TTYC_MS, flags, encoded);
    free(encoded as *mut libc::c_void);
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_rawstring(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    (*tty).flags |= 0x8 as i32;
    tty_add(tty, (*ctx).ptr as *const i8, (*ctx).num as size_t);
    tty_invalidate(tty);
}
#[no_mangle]
pub unsafe extern "C" fn tty_cmd_syncstart(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    if (*ctx).num == 0x11 as i32 as u32 {
        tty_sync_start(tty);
    } else if !(*ctx).num & 0x10 as i32 as u32 != 0 {
        if (*ctx).num != 0 || ((*(*tty).client).overlay_draw).is_some() {
            tty_sync_start(tty);
        }
    }
}
#[no_mangle]
pub unsafe extern "C" fn tty_cell(
    mut tty: *mut tty,
    mut gc: *const grid_cell,
    mut defaults: *const grid_cell,
    mut palette: *mut colour_palette,
    mut hl: *mut hyperlinks,
) {
    let mut gcp: *const grid_cell = std::ptr::null::<grid_cell>();
    if (*(*tty).term).flags & 0x2 as i32 != 0
        && (*tty).cy == ((*tty).sy).wrapping_sub(1 as i32 as u32)
        && (*tty).cx == ((*tty).sx).wrapping_sub(1 as i32 as u32)
    {
        return;
    }
    if (*gc).flags as i32 & 0x4 as i32 != 0 {
        return;
    }
    gcp = tty_check_codeset(tty, gc);
    tty_attributes(tty, gcp, defaults, palette, hl);
    if (*gcp).data.size as i32 == 1 as i32 {
        tty_attributes(tty, gcp, defaults, palette, hl);
        if (*((*gcp).data.data).as_ptr() as i32) < 0x20 as i32
            || *((*gcp).data.data).as_ptr() as i32 == 0x7f as i32
        {
            return;
        }
        tty_putc(tty, *((*gcp).data.data).as_ptr());
        return;
    }
    tty_putn(
        tty,
        ((*gcp).data.data).as_ptr() as *const libc::c_void,
        (*gcp).data.size as size_t,
        (*gcp).data.width as u_int,
    );
}
#[no_mangle]
pub unsafe extern "C" fn tty_reset(mut tty: *mut tty) {
    let mut gc: *mut grid_cell = &mut (*tty).cell;
    if grid_cells_equal(gc, &grid_default_cell) == 0 {
        if (*gc).link != 0 as i32 as u32 {
            tty_putcode_ss(
                tty,
                TTYC_HLS,
                b"\0" as *const u8 as *const i8,
                b"\0" as *const u8 as *const i8,
            );
        }
        if (*gc).attr as i32 & 0x80 as i32 != 0 && tty_acs_needed(tty) != 0 {
            tty_putcode(tty, TTYC_RMACS);
        }
        tty_putcode(tty, TTYC_SGR0);
        memcpy(
            gc as *mut libc::c_void,
            &grid_default_cell as *const grid_cell as *const libc::c_void,
            ::core::mem::size_of::<grid_cell>() as u64,
        );
    }
    memcpy(
        &mut (*tty).last_cell as *mut grid_cell as *mut libc::c_void,
        &grid_default_cell as *const grid_cell as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
}
#[no_mangle]
pub unsafe extern "C" fn tty_invalidate(mut tty: *mut tty) {
    memcpy(
        &mut (*tty).cell as *mut grid_cell as *mut libc::c_void,
        &grid_default_cell as *const grid_cell as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
    memcpy(
        &mut (*tty).last_cell as *mut grid_cell as *mut libc::c_void,
        &grid_default_cell as *const grid_cell as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
    (*tty).cy = (2147483647 as i32 as u32)
        .wrapping_mul(2 as u32)
        .wrapping_add(1 as u32);
    (*tty).cx = (*tty).cy;
    (*tty).rleft = (2147483647 as i32 as u32)
        .wrapping_mul(2 as u32)
        .wrapping_add(1 as u32);
    (*tty).rupper = (*tty).rleft;
    (*tty).rright = (2147483647 as i32 as u32)
        .wrapping_mul(2 as u32)
        .wrapping_add(1 as u32);
    (*tty).rlower = (*tty).rright;
    if (*tty).flags & 0x10 as i32 != 0 {
        if (*(*tty).term).flags & 0x4 as i32 != 0 {
            tty_putcode(tty, TTYC_ENMG);
        }
        tty_putcode(tty, TTYC_SGR0);
        (*tty).mode = 0xffffff as i32;
        tty_update_mode(tty, 0x1 as i32, std::ptr::null_mut::<screen>());
        tty_cursor(tty, 0 as i32 as u_int, 0 as i32 as u_int);
        tty_region_off(tty);
        tty_margin_off(tty);
    } else {
        (*tty).mode = 0x1 as i32;
    };
}
#[no_mangle]
pub unsafe extern "C" fn tty_region_off(mut tty: *mut tty) {
    tty_region(
        tty,
        0 as i32 as u_int,
        ((*tty).sy).wrapping_sub(1 as i32 as u32),
    );
}
unsafe extern "C" fn tty_region_pane(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut rupper: u_int,
    mut rlower: u_int,
) {
    tty_region(
        tty,
        ((*ctx).yoff).wrapping_add(rupper).wrapping_sub((*ctx).woy),
        ((*ctx).yoff).wrapping_add(rlower).wrapping_sub((*ctx).woy),
    );
}
unsafe extern "C" fn tty_region(mut tty: *mut tty, mut rupper: u_int, mut rlower: u_int) {
    if (*tty).rlower == rlower && (*tty).rupper == rupper {
        return;
    }
    if tty_term_has((*tty).term, TTYC_CSR) == 0 {
        return;
    }
    (*tty).rupper = rupper;
    (*tty).rlower = rlower;
    if (*tty).cx >= (*tty).sx {
        if (*tty).cy
            == (2147483647 as i32 as u32)
                .wrapping_mul(2 as u32)
                .wrapping_add(1 as u32)
        {
            tty_cursor(tty, 0 as i32 as u_int, 0 as i32 as u_int);
        } else {
            tty_cursor(tty, 0 as i32 as u_int, (*tty).cy);
        }
    }
    tty_putcode_ii(
        tty,
        TTYC_CSR,
        (*tty).rupper as i32,
        (*tty).rlower as i32,
    );
    (*tty).cy = (2147483647 as i32 as u32)
        .wrapping_mul(2 as u32)
        .wrapping_add(1 as u32);
    (*tty).cx = (*tty).cy;
}
#[no_mangle]
pub unsafe extern "C" fn tty_margin_off(mut tty: *mut tty) {
    tty_margin(
        tty,
        0 as i32 as u_int,
        ((*tty).sx).wrapping_sub(1 as i32 as u32),
    );
}
unsafe extern "C" fn tty_margin_pane(mut tty: *mut tty, mut ctx: *const tty_ctx) {
    tty_margin(
        tty,
        ((*ctx).xoff).wrapping_sub((*ctx).wox),
        ((*ctx).xoff)
            .wrapping_add((*ctx).sx)
            .wrapping_sub(1 as i32 as u32)
            .wrapping_sub((*ctx).wox),
    );
}
unsafe extern "C" fn tty_margin(mut tty: *mut tty, mut rleft: u_int, mut rright: u_int) {
    if (*(*tty).term).flags & 0x4 as i32 == 0 {
        return;
    }
    if (*tty).rleft == rleft && (*tty).rright == rright {
        return;
    }
    tty_putcode_ii(
        tty,
        TTYC_CSR,
        (*tty).rupper as i32,
        (*tty).rlower as i32,
    );
    (*tty).rleft = rleft;
    (*tty).rright = rright;
    if rleft == 0 as i32 as u32
        && rright == ((*tty).sx).wrapping_sub(1 as i32 as u32)
    {
        tty_putcode(tty, TTYC_CLMG);
    } else {
        tty_putcode_ii(tty, TTYC_CMG, rleft as i32, rright as i32);
    }
    (*tty).cy = (2147483647 as i32 as u32)
        .wrapping_mul(2 as u32)
        .wrapping_add(1 as u32);
    (*tty).cx = (*tty).cy;
}
unsafe extern "C" fn tty_cursor_pane_unless_wrap(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut cx: u_int,
    mut cy: u_int,
) {
    if (*ctx).wrapped == 0
        || !((*ctx).xoff == 0 as i32 as u32 && (*ctx).sx >= (*tty).sx)
        || (*(*tty).term).flags & 0x2 as i32 != 0
        || ((*ctx).xoff).wrapping_add(cx) != 0 as i32 as u32
        || ((*ctx).yoff).wrapping_add(cy)
            != ((*tty).cy).wrapping_add(1 as i32 as u32)
        || (*tty).cx < (*tty).sx
        || (*tty).cy == (*tty).rlower
    {
        tty_cursor_pane(tty, ctx, cx, cy);
    } else {
        log_debug(
            b"%s: will wrap at %u,%u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 28], &[i8; 28]>(
                b"tty_cursor_pane_unless_wrap\0",
            ))
            .as_ptr(),
            (*tty).cx,
            (*tty).cy,
        );
    };
}
unsafe extern "C" fn tty_cursor_pane(
    mut tty: *mut tty,
    mut ctx: *const tty_ctx,
    mut cx: u_int,
    mut cy: u_int,
) {
    tty_cursor(
        tty,
        ((*ctx).xoff).wrapping_add(cx).wrapping_sub((*ctx).wox),
        ((*ctx).yoff).wrapping_add(cy).wrapping_sub((*ctx).woy),
    );
}
#[no_mangle]
pub unsafe extern "C" fn tty_cursor(mut tty: *mut tty, mut cx: u_int, mut cy: u_int) {
    let mut current_block: u64;
    let mut term: *mut tty_term = (*tty).term;
    let mut thisx: u_int = 0;
    let mut thisy: u_int = 0;
    let mut change: i32 = 0;
    if (*tty).flags & 0x80 as i32 != 0 {
        return;
    }
    thisx = (*tty).cx;
    thisy = (*tty).cy;
    if cx == thisx && cy == thisy && cx == (*tty).sx {
        return;
    }
    if cx > ((*tty).sx).wrapping_sub(1 as i32 as u32) {
        log_debug(
            b"%s: x too big %u > %u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 11], &[i8; 11]>(b"tty_cursor\0")).as_ptr(),
            cx,
            ((*tty).sx).wrapping_sub(1 as i32 as u32),
        );
        cx = ((*tty).sx).wrapping_sub(1 as i32 as u32);
    }
    if cx == thisx && cy == thisy {
        return;
    }
    if thisx > ((*tty).sx).wrapping_sub(1 as i32 as u32) {
        current_block = 16274275253645873319;
    } else if cx == 0 as i32 as u32
        && cy == 0 as i32 as u32
        && tty_term_has(term, TTYC_HOME) != 0
    {
        tty_putcode(tty, TTYC_HOME);
        current_block = 17422582042514589789;
    } else if cx == 0 as i32 as u32
        && cy == thisy.wrapping_add(1 as i32 as u32)
        && thisy != (*tty).rlower
        && ((*(*tty).term).flags & 0x4 as i32 == 0
            || (*tty).rleft == 0 as i32 as u32)
    {
        tty_putc(tty, '\r' as i32 as u_char);
        tty_putc(tty, '\n' as i32 as u_char);
        current_block = 17422582042514589789;
    } else if cy == thisy {
        if cx == 0 as i32 as u32
            && ((*(*tty).term).flags & 0x4 as i32 == 0
                || (*tty).rleft == 0 as i32 as u32)
        {
            tty_putc(tty, '\r' as i32 as u_char);
            current_block = 17422582042514589789;
        } else if cx == thisx.wrapping_sub(1 as i32 as u32)
            && tty_term_has(term, TTYC_CUB1) != 0
        {
            tty_putcode(tty, TTYC_CUB1);
            current_block = 17422582042514589789;
        } else if cx == thisx.wrapping_add(1 as i32 as u32)
            && tty_term_has(term, TTYC_CUF1) != 0
        {
            tty_putcode(tty, TTYC_CUF1);
            current_block = 17422582042514589789;
        } else {
            change = thisx.wrapping_sub(cx) as i32;
            if abs(change) as u_int > cx && tty_term_has(term, TTYC_HPA) != 0 {
                tty_putcode_i(tty, TTYC_HPA, cx as i32);
                current_block = 17422582042514589789;
            } else if change > 0 as i32
                && tty_term_has(term, TTYC_CUB) != 0
                && (*(*tty).term).flags & 0x4 as i32 == 0
            {
                if change == 2 as i32 && tty_term_has(term, TTYC_CUB1) != 0 {
                    tty_putcode(tty, TTYC_CUB1);
                    tty_putcode(tty, TTYC_CUB1);
                } else {
                    tty_putcode_i(tty, TTYC_CUB, change);
                }
                current_block = 17422582042514589789;
            } else if change < 0 as i32
                && tty_term_has(term, TTYC_CUF) != 0
                && (*(*tty).term).flags & 0x4 as i32 == 0
            {
                tty_putcode_i(tty, TTYC_CUF, -change);
                current_block = 17422582042514589789;
            } else {
                current_block = 16274275253645873319;
            }
        }
    } else if cx == thisx {
        if thisy != (*tty).rupper
            && cy == thisy.wrapping_sub(1 as i32 as u32)
            && tty_term_has(term, TTYC_CUU1) != 0
        {
            tty_putcode(tty, TTYC_CUU1);
            current_block = 17422582042514589789;
        } else if thisy != (*tty).rlower
            && cy == thisy.wrapping_add(1 as i32 as u32)
            && tty_term_has(term, TTYC_CUD1) != 0
        {
            tty_putcode(tty, TTYC_CUD1);
            current_block = 17422582042514589789;
        } else {
            change = thisy.wrapping_sub(cy) as i32;
            if abs(change) as u_int > cy
                || change < 0 as i32
                    && cy.wrapping_sub(change as u32) > (*tty).rlower
                || change > 0 as i32
                    && cy.wrapping_sub(change as u32) < (*tty).rupper
            {
                if tty_term_has(term, TTYC_VPA) != 0 {
                    tty_putcode_i(tty, TTYC_VPA, cy as i32);
                    current_block = 17422582042514589789;
                } else {
                    current_block = 16274275253645873319;
                }
            } else if change > 0 as i32 && tty_term_has(term, TTYC_CUU) != 0 {
                tty_putcode_i(tty, TTYC_CUU, change);
                current_block = 17422582042514589789;
            } else if change < 0 as i32 && tty_term_has(term, TTYC_CUD) != 0 {
                tty_putcode_i(tty, TTYC_CUD, -change);
                current_block = 17422582042514589789;
            } else {
                current_block = 16274275253645873319;
            }
        }
    } else {
        current_block = 16274275253645873319;
    }
    match current_block {
        16274275253645873319 => {
            tty_putcode_ii(tty, TTYC_CUP, cy as i32, cx as i32);
        }
        _ => {}
    }
    (*tty).cx = cx;
    (*tty).cy = cy;
}
unsafe extern "C" fn tty_hyperlink(
    mut tty: *mut tty,
    mut gc: *const grid_cell,
    mut hl: *mut hyperlinks,
) {
    let mut uri: *const i8 = std::ptr::null::<i8>();
    let mut id: *const i8 = std::ptr::null::<i8>();
    if (*gc).link == (*tty).cell.link {
        return;
    }
    (*tty).cell.link = (*gc).link;
    if hl.is_null() {
        return;
    }
    if (*gc).link == 0 as i32 as u32
        || hyperlinks_get(
            hl,
            (*gc).link,
            &mut uri,
            std::ptr::null_mut::<*const i8>(),
            &mut id,
        ) == 0
    {
        tty_putcode_ss(
            tty,
            TTYC_HLS,
            b"\0" as *const u8 as *const i8,
            b"\0" as *const u8 as *const i8,
        );
    } else {
        tty_putcode_ss(tty, TTYC_HLS, id, uri);
    };
}
#[no_mangle]
pub unsafe extern "C" fn tty_attributes(
    mut tty: *mut tty,
    mut gc: *const grid_cell,
    mut defaults: *const grid_cell,
    mut palette: *mut colour_palette,
    mut hl: *mut hyperlinks,
) {
    let mut tc: *mut grid_cell = &mut (*tty).cell;
    let mut gc2: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut changed: i32 = 0;
    memcpy(
        &mut gc2 as *mut grid_cell as *mut libc::c_void,
        gc as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
    if !((*gc).flags as i32) & 0x20 as i32 != 0 {
        if gc2.fg == 8 as i32 {
            gc2.fg = (*defaults).fg;
        }
        if gc2.bg == 8 as i32 {
            gc2.bg = (*defaults).bg;
        }
    }
    if gc2.attr as i32 == (*tty).last_cell.attr as i32
        && gc2.fg == (*tty).last_cell.fg
        && gc2.bg == (*tty).last_cell.bg
        && gc2.us == (*tty).last_cell.us
        && gc2.link == (*tty).last_cell.link
    {
        return;
    }
    if tty_term_has((*tty).term, TTYC_SETAB) == 0 {
        if gc2.attr as i32 & 0x10 as i32 != 0 {
            if gc2.fg != 7 as i32
                && !(gc2.fg == 8 as i32 || gc2.fg == 9 as i32)
            {
                gc2.attr = (gc2.attr as i32 & !(0x10 as i32)) as u_short;
            }
        } else if gc2.bg != 0 as i32
            && !(gc2.bg == 8 as i32 || gc2.bg == 9 as i32)
        {
            gc2.attr = (gc2.attr as i32 | 0x10 as i32) as u_short;
        }
    }
    tty_check_fg(tty, palette, &mut gc2);
    tty_check_bg(tty, palette, &mut gc2);
    tty_check_us(tty, palette, &mut gc2);
    if (*tc).attr as i32 & !(gc2.attr as i32) != 0
        || (*tc).us != gc2.us && gc2.us == 0 as i32
    {
        tty_reset(tty);
    }
    tty_colours(tty, &mut gc2);
    changed = gc2.attr as i32 & !((*tc).attr as i32);
    (*tc).attr = gc2.attr;
    if changed & 0x1 as i32 != 0 {
        tty_putcode(tty, TTYC_BOLD);
    }
    if changed & 0x2 as i32 != 0 {
        tty_putcode(tty, TTYC_DIM);
    }
    if changed & 0x40 as i32 != 0 {
        tty_set_italics(tty);
    }
    if changed
        & (0x4 as i32
            | 0x200 as i32
            | 0x400 as i32
            | 0x800 as i32
            | 0x1000 as i32)
        != 0
    {
        if changed & 0x4 as i32 != 0 || tty_term_has((*tty).term, TTYC_SMULX) == 0 {
            tty_putcode(tty, TTYC_SMUL);
        } else if changed & 0x200 as i32 != 0 {
            tty_putcode_i(tty, TTYC_SMULX, 2 as i32);
        } else if changed & 0x400 as i32 != 0 {
            tty_putcode_i(tty, TTYC_SMULX, 3 as i32);
        } else if changed & 0x800 as i32 != 0 {
            tty_putcode_i(tty, TTYC_SMULX, 4 as i32);
        } else if changed & 0x1000 as i32 != 0 {
            tty_putcode_i(tty, TTYC_SMULX, 5 as i32);
        }
    }
    if changed & 0x8 as i32 != 0 {
        tty_putcode(tty, TTYC_BLINK);
    }
    if changed & 0x10 as i32 != 0 {
        if tty_term_has((*tty).term, TTYC_REV) != 0 {
            tty_putcode(tty, TTYC_REV);
        } else if tty_term_has((*tty).term, TTYC_SMSO) != 0 {
            tty_putcode(tty, TTYC_SMSO);
        }
    }
    if changed & 0x20 as i32 != 0 {
        tty_putcode(tty, TTYC_INVIS);
    }
    if changed & 0x100 as i32 != 0 {
        tty_putcode(tty, TTYC_SMXX);
    }
    if changed & 0x2000 as i32 != 0 {
        tty_putcode(tty, TTYC_SMOL);
    }
    if changed & 0x80 as i32 != 0 && tty_acs_needed(tty) != 0 {
        tty_putcode(tty, TTYC_SMACS);
    }
    tty_hyperlink(tty, gc, hl);
    memcpy(
        &mut (*tty).last_cell as *mut grid_cell as *mut libc::c_void,
        &mut gc2 as *mut grid_cell as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
}
unsafe extern "C" fn tty_colours(mut tty: *mut tty, mut gc: *const grid_cell) {
    let mut tc: *mut grid_cell = &mut (*tty).cell;
    if (*gc).fg == (*tc).fg && (*gc).bg == (*tc).bg && (*gc).us == (*tc).us {
        return;
    }
    if (*gc).fg == 8 as i32
        || (*gc).fg == 9 as i32
        || ((*gc).bg == 8 as i32 || (*gc).bg == 9 as i32)
    {
        if tty_term_flag((*tty).term, TTYC_AX) == 0 {
            tty_reset(tty);
        } else {
            if ((*gc).fg == 8 as i32 || (*gc).fg == 9 as i32)
                && !((*tc).fg == 8 as i32 || (*tc).fg == 9 as i32)
            {
                tty_puts(tty, b"\x1B[39m\0" as *const u8 as *const i8);
                (*tc).fg = (*gc).fg;
            }
            if ((*gc).bg == 8 as i32 || (*gc).bg == 9 as i32)
                && !((*tc).bg == 8 as i32 || (*tc).bg == 9 as i32)
            {
                tty_puts(tty, b"\x1B[49m\0" as *const u8 as *const i8);
                (*tc).bg = (*gc).bg;
            }
        }
    }
    if !((*gc).fg == 8 as i32 || (*gc).fg == 9 as i32) && (*gc).fg != (*tc).fg {
        tty_colours_fg(tty, gc);
    }
    if !((*gc).bg == 8 as i32 || (*gc).bg == 9 as i32) && (*gc).bg != (*tc).bg {
        tty_colours_bg(tty, gc);
    }
    if (*gc).us != (*tc).us {
        tty_colours_us(tty, gc);
    }
}
unsafe extern "C" fn tty_check_fg(
    mut tty: *mut tty,
    mut palette: *mut colour_palette,
    mut gc: *mut grid_cell,
) {
    let mut r: u_char = 0;
    let mut g: u_char = 0;
    let mut b: u_char = 0;
    let mut colours: u_int = 0;
    let mut c: i32 = 0;
    if !((*gc).flags as i32) & 0x20 as i32 != 0 {
        c = (*gc).fg;
        if c < 8 as i32
            && (*gc).attr as i32 & 0x1 as i32 != 0
            && tty_term_has((*tty).term, TTYC_NOBR) == 0
        {
            c += 90 as i32;
        }
        c = colour_palette_get(palette, c);
        if c != -(1 as i32) {
            (*gc).fg = c;
        }
    }
    if (*gc).fg & 0x2000000 as i32 != 0 {
        if (*(*tty).term).flags & 0x10 as i32 != 0 {
            return;
        }
        colour_split_rgb((*gc).fg, &mut r, &mut g, &mut b);
        (*gc).fg = colour_find_rgb(r, g, b);
    }
    if (*(*tty).term).flags & 0x1 as i32 != 0 {
        colours = 256 as i32 as u_int;
    } else {
        colours = tty_term_number((*tty).term, TTYC_COLORS) as u_int;
    }
    if (*gc).fg & 0x1000000 as i32 != 0 {
        if colours < 256 as i32 as u32 {
            (*gc).fg = colour_256to16((*gc).fg);
            if (*gc).fg & 8 as i32 != 0 {
                (*gc).fg &= 7 as i32;
                if colours >= 16 as i32 as u32 {
                    (*gc).fg += 90 as i32;
                }
            }
        }
        return;
    }
    if (*gc).fg >= 90 as i32
        && (*gc).fg <= 97 as i32
        && colours < 16 as i32 as u32
    {
        (*gc).fg -= 90 as i32;
        (*gc).attr = ((*gc).attr as i32 | 0x1 as i32) as u_short;
    }
}
unsafe extern "C" fn tty_check_bg(
    mut tty: *mut tty,
    mut palette: *mut colour_palette,
    mut gc: *mut grid_cell,
) {
    let mut r: u_char = 0;
    let mut g: u_char = 0;
    let mut b: u_char = 0;
    let mut colours: u_int = 0;
    let mut c: i32 = 0;
    if !((*gc).flags as i32) & 0x20 as i32 != 0 {
        c = colour_palette_get(palette, (*gc).bg);
        if c != -(1 as i32) {
            (*gc).bg = c;
        }
    }
    if (*gc).bg & 0x2000000 as i32 != 0 {
        if (*(*tty).term).flags & 0x10 as i32 != 0 {
            return;
        }
        colour_split_rgb((*gc).bg, &mut r, &mut g, &mut b);
        (*gc).bg = colour_find_rgb(r, g, b);
    }
    if (*(*tty).term).flags & 0x1 as i32 != 0 {
        colours = 256 as i32 as u_int;
    } else {
        colours = tty_term_number((*tty).term, TTYC_COLORS) as u_int;
    }
    if (*gc).bg & 0x1000000 as i32 != 0 {
        if colours < 256 as i32 as u32 {
            (*gc).bg = colour_256to16((*gc).bg);
            if (*gc).bg & 8 as i32 != 0 {
                (*gc).bg &= 7 as i32;
                if colours >= 16 as i32 as u32 {
                    (*gc).bg += 90 as i32;
                }
            }
        }
        return;
    }
    if (*gc).bg >= 90 as i32
        && (*gc).bg <= 97 as i32
        && colours < 16 as i32 as u32
    {
        (*gc).bg -= 90 as i32;
    }
}
unsafe extern "C" fn tty_check_us(
    mut tty: *mut tty,
    mut palette: *mut colour_palette,
    mut gc: *mut grid_cell,
) {
    let mut c: i32 = 0;
    if !((*gc).flags as i32) & 0x20 as i32 != 0 {
        c = colour_palette_get(palette, (*gc).us);
        if c != -(1 as i32) {
            (*gc).us = c;
        }
    }
    if tty_term_has((*tty).term, TTYC_SETULC1) == 0 {
        c = colour_force_rgb((*gc).us);
        if c == -(1 as i32) {
            (*gc).us = 8 as i32;
        } else {
            (*gc).us = c;
        }
    }
}
unsafe extern "C" fn tty_colours_fg(mut tty: *mut tty, mut gc: *const grid_cell) {
    let mut tc: *mut grid_cell = &mut (*tty).cell;
    let mut s: [i8; 32] = [0; 32];
    if (*tty).cell.fg >= 90 as i32
        && (*tty).cell.bg <= 97 as i32
        && ((*gc).fg < 90 as i32 || (*gc).fg > 97 as i32)
    {
        tty_reset(tty);
    }
    if (*gc).fg & 0x2000000 as i32 != 0 || (*gc).fg & 0x1000000 as i32 != 0 {
        if tty_try_colour(tty, (*gc).fg, b"38\0" as *const u8 as *const i8)
            != 0 as i32
        {
            return;
        }
    } else if (*gc).fg >= 90 as i32 && (*gc).fg <= 97 as i32 {
        if (*(*tty).term).flags & 0x1 as i32 != 0 {
            xsnprintf(
                s.as_mut_ptr(),
                ::core::mem::size_of::<[i8; 32]>() as u64,
                b"\x1B[%dm\0" as *const u8 as *const i8,
                (*gc).fg,
            );
            tty_puts(tty, s.as_mut_ptr());
        } else {
            tty_putcode_i(
                tty,
                TTYC_SETAF,
                (*gc).fg - 90 as i32 + 8 as i32,
            );
        }
    } else {
        tty_putcode_i(tty, TTYC_SETAF, (*gc).fg);
    }
    (*tc).fg = (*gc).fg;
}
unsafe extern "C" fn tty_colours_bg(mut tty: *mut tty, mut gc: *const grid_cell) {
    let mut tc: *mut grid_cell = &mut (*tty).cell;
    let mut s: [i8; 32] = [0; 32];
    if (*gc).bg & 0x2000000 as i32 != 0 || (*gc).bg & 0x1000000 as i32 != 0 {
        if tty_try_colour(tty, (*gc).bg, b"48\0" as *const u8 as *const i8)
            != 0 as i32
        {
            return;
        }
    } else if (*gc).bg >= 90 as i32 && (*gc).bg <= 97 as i32 {
        if (*(*tty).term).flags & 0x1 as i32 != 0 {
            xsnprintf(
                s.as_mut_ptr(),
                ::core::mem::size_of::<[i8; 32]>() as u64,
                b"\x1B[%dm\0" as *const u8 as *const i8,
                (*gc).bg + 10 as i32,
            );
            tty_puts(tty, s.as_mut_ptr());
        } else {
            tty_putcode_i(
                tty,
                TTYC_SETAB,
                (*gc).bg - 90 as i32 + 8 as i32,
            );
        }
    } else {
        tty_putcode_i(tty, TTYC_SETAB, (*gc).bg);
    }
    (*tc).bg = (*gc).bg;
}
unsafe extern "C" fn tty_colours_us(mut tty: *mut tty, mut gc: *const grid_cell) {
    let mut tc: *mut grid_cell = &mut (*tty).cell;
    let mut c: u_int = 0;
    let mut r: u_char = 0;
    let mut g: u_char = 0;
    let mut b: u_char = 0;
    if (*gc).us == 8 as i32 || (*gc).us == 9 as i32 {
        tty_putcode(tty, TTYC_OL);
    } else {
        if !(*gc).us & 0x2000000 as i32 != 0 {
            c = (*gc).us as u_int;
            if !c & 0x1000000 as i32 as u32 != 0
                && (c >= 90 as i32 as u32
                    && c <= 97 as i32 as u32)
            {
                c = (c as u32).wrapping_sub(82 as i32 as u32) as u_int
                    as u_int;
            }
            tty_putcode_i(
                tty,
                TTYC_SETULC1,
                (c & !(0x1000000 as i32) as u32) as i32,
            );
            return;
        }
        colour_split_rgb((*gc).us, &mut r, &mut g, &mut b);
        c = (65536 as i32 * r as i32
            + 256 as i32 * g as i32
            + b as i32) as u_int;
        if tty_term_has((*tty).term, TTYC_SETULC) != 0 {
            tty_putcode_i(tty, TTYC_SETULC, c as i32);
        } else if tty_term_has((*tty).term, TTYC_SETAL) != 0
            && tty_term_has((*tty).term, TTYC_RGB) != 0
        {
            tty_putcode_i(tty, TTYC_SETAL, c as i32);
        }
    }
    (*tc).us = (*gc).us;
}
unsafe extern "C" fn tty_try_colour(
    mut tty: *mut tty,
    mut colour: i32,
    mut type_0: *const i8,
) -> i32 {
    let mut r: u_char = 0;
    let mut g: u_char = 0;
    let mut b: u_char = 0;
    if colour & 0x1000000 as i32 != 0 {
        if *type_0 as i32 == '3' as i32 && tty_term_has((*tty).term, TTYC_SETAF) != 0 {
            tty_putcode_i(tty, TTYC_SETAF, colour & 0xff as i32);
        } else if tty_term_has((*tty).term, TTYC_SETAB) != 0 {
            tty_putcode_i(tty, TTYC_SETAB, colour & 0xff as i32);
        }
        return 0 as i32;
    }
    if colour & 0x2000000 as i32 != 0 {
        colour_split_rgb(colour & 0xffffff as i32, &mut r, &mut g, &mut b);
        if *type_0 as i32 == '3' as i32 && tty_term_has((*tty).term, TTYC_SETRGBF) != 0 {
            tty_putcode_iii(
                tty,
                TTYC_SETRGBF,
                r as i32,
                g as i32,
                b as i32,
            );
        } else if tty_term_has((*tty).term, TTYC_SETRGBB) != 0 {
            tty_putcode_iii(
                tty,
                TTYC_SETRGBB,
                r as i32,
                g as i32,
                b as i32,
            );
        }
        return 0 as i32;
    }
    return -(1 as i32);
}
unsafe extern "C" fn tty_window_default_style(mut gc: *mut grid_cell, mut wp: *mut window_pane) {
    memcpy(
        gc as *mut libc::c_void,
        &grid_default_cell as *const grid_cell as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
    (*gc).fg = (*wp).palette.fg;
    (*gc).bg = (*wp).palette.bg;
}
#[no_mangle]
pub unsafe extern "C" fn tty_default_colours(mut gc: *mut grid_cell, mut wp: *mut window_pane) {
    let mut oo: *mut options = (*wp).options;
    let mut ft: *mut format_tree = std::ptr::null_mut::<format_tree>();
    memcpy(
        gc as *mut libc::c_void,
        &grid_default_cell as *const grid_cell as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
    if (*wp).flags & 0x1000 as i32 != 0 {
        log_debug(
            b"%%%u: style changed\0" as *const u8 as *const i8,
            (*wp).id,
        );
        (*wp).flags &= !(0x1000 as i32);
        ft = format_create(
            std::ptr::null_mut::<client>(),
            std::ptr::null_mut::<cmdq_item>(),
            (0x80000000 as u32 | (*wp).id) as i32,
            0x4 as i32,
        );
        format_defaults(
            ft,
            std::ptr::null_mut::<client>(),
            std::ptr::null_mut::<session>(),
            std::ptr::null_mut::<winlink>(),
            wp,
        );
        tty_window_default_style(&mut (*wp).cached_active_gc, wp);
        style_add(
            &mut (*wp).cached_active_gc,
            oo,
            b"window-active-style\0" as *const u8 as *const i8,
            ft,
        );
        tty_window_default_style(&mut (*wp).cached_gc, wp);
        style_add(
            &mut (*wp).cached_gc,
            oo,
            b"window-style\0" as *const u8 as *const i8,
            ft,
        );
        format_free(ft);
    }
    if (*gc).fg == 8 as i32 {
        if wp == (*(*wp).window).active && (*wp).cached_active_gc.fg != 8 as i32 {
            (*gc).fg = (*wp).cached_active_gc.fg;
        } else {
            (*gc).fg = (*wp).cached_gc.fg;
        }
    }
    if (*gc).bg == 8 as i32 {
        if wp == (*(*wp).window).active && (*wp).cached_active_gc.bg != 8 as i32 {
            (*gc).bg = (*wp).cached_active_gc.bg;
        } else {
            (*gc).bg = (*wp).cached_gc.bg;
        }
    }
}
unsafe extern "C" fn tty_default_attributes(
    mut tty: *mut tty,
    mut defaults: *const grid_cell,
    mut palette: *mut colour_palette,
    mut bg: u_int,
    mut hl: *mut hyperlinks,
) {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    memcpy(
        &mut gc as *mut grid_cell as *mut libc::c_void,
        &grid_default_cell as *const grid_cell as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
    gc.bg = bg as i32;
    tty_attributes(tty, &mut gc, defaults, palette, hl);
}
unsafe extern "C" fn tty_clipboard_query_callback(
    mut fd: i32,
    mut events: i16,
    mut data: *mut libc::c_void,
) {
    let mut tty: *mut tty = data as *mut tty;
    let mut c: *mut client = (*tty).client;
    (*c).flags =
        ((*c).flags as u64 & !(0x800000000 as u64)) as uint64_t;
    free((*c).clipboard_panes as *mut libc::c_void);
    (*c).clipboard_panes = std::ptr::null_mut::<u_int>();
    (*c).clipboard_npanes = 0 as i32 as u_int;
    (*tty).flags &= !(0x40 as i32);
}
#[no_mangle]
pub unsafe extern "C" fn tty_clipboard_query(mut tty: *mut tty) {
    let mut tv: timeval = {
        let mut init = timeval {
            tv_sec: 5 as i32 as __time_t,
            tv_usec: 0,
        };
        init
    };
    if !(*tty).flags & 0x10 as i32 != 0 || (*tty).flags & 0x40 as i32 != 0 {
        return;
    }
    tty_putcode_ss(
        tty,
        TTYC_MS,
        b"\0" as *const u8 as *const i8,
        b"?\0" as *const u8 as *const i8,
    );
    (*tty).flags |= 0x40 as i32;
    event_set(
        &mut (*tty).clipboard_timer,
        -(1 as i32),
        0 as i32 as i16,
        Some(
            tty_clipboard_query_callback
                as unsafe extern "C" fn(i32, i16, *mut libc::c_void) -> (),
        ),
        tty as *mut libc::c_void,
    );
    event_add(&mut (*tty).clipboard_timer, &mut tv);
}
